5
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();
});

上面的代码在 Safari、Chrome 和 Opera 中运行良好,但 firefox 忽略了form.submit();. console.log('...');我通过在提交调用的上方和下方添加来测试上面的代码,并且它执行没有错误。我也试过打电话$(form).submit();,我得到了同样不想要的结果。

有没有人遇到过这个问题,或者有解决方案?

4

1 回答 1

5

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

于 2012-04-11T15:09:26.120 回答