Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Form.submit() on Chrome works but not in Firefox

I have the following function that collects data from a page, stuffs them all into the 'data' variable, appends it to a form then submits it.

 $(document).ready(function () {
$('#content-tab .submit').click(function () {
    var data = {champion: window.selectedChampion, runes: runes, masteries: masteries, items: items, skillingOrders: skillingOrders, chapters: chapters, title: $('#guide_title').val()};
            data = JSON.stringify(data); 
            $("<form method='post'>").append($('<input type="hidden" name="data" id="data">').val(data)).submit();
    });
});

There is a div on the page that triggers this when clicked on:

<div class='button pointer submit'>Submit</div>

All is well when tested in Chrome. The form submits then redirects to a page, just as planned. But while testing in Firefox (v. 5 and 6), clicking on the div does nothing. Nada. Zilch. I wonder what went wrong in Firefox? Any help would be highly appreciated. Thank you.

like image 455
druesome Avatar asked Aug 19 '11 04:08

druesome


People also ask

Why form is not submitting in jQuery?

The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.

How can we submit a form using jQuery?

Forms can be submitted either by clicking an explicit <input type="submit"> , <input type="image"> , or <button type="submit"> , or by pressing Enter when certain form elements have focus.

What does jQuery submit do?

jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.


1 Answers

I would try adding the form to the DOM before submitting.

$('#content-tab .submit').click(function() {

    var data = {
        champion: window.selectedChampion,
        runes: runes,
        masteries: masteries,
        items: items,
        skillingOrders: skillingOrders,
        chapters: chapters,
        title: $('#guide_title').val()
    };
    data = JSON.stringify(data);
    var $form = $("<form method='post'>").append($('<input type="hidden" name="data" id="data">').val(data));
    $form.appendTo("body").submit();

});
like image 131
karim79 Avatar answered Sep 19 '22 14:09

karim79