JQuery ajaxForm:如果在 beforeSubmit 中从 DOM 中删除表单,则不会提交表单

发布于 2024-11-07 12:30:52 字数 887 浏览 3 评论 0原文

如果我在 beforeSubmit 或 beforeSend 中从 DOM 中删除表单(无论哪一个),我发现在 IE 和 FF 中,永远不会发出 http 请求。调用 jquery.form.js 中的 form.submit() 第 296 行,但没有发出 http 请求。但它在 chrome 中工作正常。

示例代码:

$('#form1').ajaxForm(
    {
        beforeSubmit: function(array, matched_set, options)
        {
            // this line removes #form1 from the DOM.
            // it is still available to jquery form plugin by means of closure
            // line 296 form.submit() in jquery.form.js is hit,
            // but IE and FF never emit http request. If I remove this line, it works.
            $('#jqm_window').html(waiting_page);
        },

铬: 在此处输入图像描述 火狐浏览器: 在此处输入图像描述 使用 chrome 时,fiddler 会捕获 http 跟踪(但不会使用其他浏览器): 在此处输入图像描述

if I remove the form from the DOM in beforeSubmit or beforeSend (doesn't matter which one), I find that in both IE and FF, http request is never emitted. form.submit() line 296 in jquery.form.js is called, but no http request is emitted. It works correctly in chrome though.

Sample code:

$('#form1').ajaxForm(
    {
        beforeSubmit: function(array, matched_set, options)
        {
            // this line removes #form1 from the DOM.
            // it is still available to jquery form plugin by means of closure
            // line 296 form.submit() in jquery.form.js is hit,
            // but IE and FF never emit http request. If I remove this line, it works.
            $('#jqm_window').html(waiting_page);
        },

chrome:
enter image description here
firefox:
enter image description here
http trace is captured in fiddler when using chrome (but not with other browsers):
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油饼 2024-11-14 12:30:52

这不是 jquery 问题。如果在提交之前将表单从 DOM 中删除,则 IE、FF 不会提交表单。完整代码:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.1.min.js">
</script>
<script>
$(document).ready(function()
{
    $('#test').click(function(e)
    {
        e.preventDefault();
        var form = $('#uploadForm')[0];
        // IE and FF will not submit the form if its removed from the DOM
        // Chrome doesn't care. you will get 404 error, as it submits the form to non-existent files.php
        form.parentNode.removeChild(form);
        form.submit();
    });
});
</script>
</head>
<body>
<form id="uploadForm" action="files.php" method="POST" enctype="multipart/form-data">
    File: <input type="file" name="file" />                
    <input type="submit" value="Submit" />
</form>
<a href="#" id="test">Click to test</a>
</body>
</html>

this is not a jquery issue. IE, FF don't submit form if its removed from DOM prior to submitting. Full code:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.1.min.js">
</script>
<script>
$(document).ready(function()
{
    $('#test').click(function(e)
    {
        e.preventDefault();
        var form = $('#uploadForm')[0];
        // IE and FF will not submit the form if its removed from the DOM
        // Chrome doesn't care. you will get 404 error, as it submits the form to non-existent files.php
        form.parentNode.removeChild(form);
        form.submit();
    });
});
</script>
</head>
<body>
<form id="uploadForm" action="files.php" method="POST" enctype="multipart/form-data">
    File: <input type="file" name="file" />                
    <input type="submit" value="Submit" />
</form>
<a href="#" id="test">Click to test</a>
</body>
</html>
凉城凉梦凉人心 2024-11-14 12:30:52

我有同样的问题。我创建 jQuery 表单对象并调用提交函数,该函数在 Chrome 中工作正常,但在 Firefox 中不行。

Chrome 和 Firefox 有不同的 js 引擎,在 Firefox 中你不能提交不在 DOM 中的表单元素。

I have the same issue. I create jQuery form object and call submit function which works fine in Chrome but not in Firefox.

Chrome and Firefox have different js engines and in Firefox you cannot submit form element which is not in DOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文