火狐jQuery表单提交不起作用 [英] Firefox jQuery form submission not working

查看:177
本文介绍了火狐jQuery表单提交不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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会忽略 .submit(); 。我通过在submit调用的上面和下面添加 console.log('...'); 来测试上面的代码,并且它没有错误地执行。我还尝试调用 $(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?

推荐答案


更新与您的新代码

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

请参阅在FF中工作jsFiddle

这篇关于火狐jQuery表单提交不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆