问题
var makeField = function(name, value) {
return $('<input />').attr({
type: 'hidden',
name: name,
value: value
});
};
$('.login').on('click', function() {
var form = $('<form />').attr('method', 'POST');
form.append(makeField('n0', 'data1'));
form.append(makeField('n1', 'data2'));
form.append(makeField('n2', 'data3'));
$(document).append(form);
form.submit();
});
The above code works fine in Safari, Chrome and Opera but firefox ignores form.submit();
. I tested the above code by adding console.log('...');
above and below the submit call, and it executes without error. I also tried calling $(form).submit();
, and I get the same unwanted result.
Has anyone ran into this before, or have a solution?
回答1:
Updated with your new code
$(function() {
var makeField = function(name, value) {
return $('<input />').attr({
type: 'hidden',
name: name,
value: value
});
};
$(document).on('click', '.login', function() {
var form = $("<form />").attr({ method: "POST" }).append(
makeField('n0', 'data1'),
makeField('n1', 'data2'),
makeField('n2', 'data3')
);
// just adding a callback on submit here to show it works
form.submit(function(e){ alert("Submitting Form"); });
$("body").append(form);
form.submit();
});
})
See WORKING jsFiddle in FF HERE
来源:https://stackoverflow.com/questions/10108330/firefox-jquery-form-submission-not-working