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: 在此处输入图像描述 firefox: 在此处输入图像描述 使用 chrome 时在 fiddler 中捕获 http 跟踪(但不使用其他浏览器): 在此处输入图像描述

4

2 回答 2

1

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

<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>
于 2011-05-17T20:35:55.560 回答
0

我有同样的问题。我创建了 jQuery 表单对象并调用了在 Chrome 中运行良好但在 Firefox 中运行良好的提交函数。

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

于 2013-09-10T09:37:08.557 回答