Firefox jQuery form submission not working

倖福魔咒の 提交于 2020-01-22 20:41:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!