1

我最近为文件上传实现了一个 jQuery / iframe 解决方案,并且遇到了一个相当烦人的问题(当然只在 IE 中)。

单击我的上传按钮并选择要上传的文件后,我会自动将包含该文件的表单提交到我的 iframe。但是 - 我需要单击页面上的任意位置,以便提交处理并触发我的控制器中的操作。

我相信这是 iframe 获得焦点的问题,这似乎阻止了“更改”功能完成执行,但我并不完全确定。iframe 可能需要某种类型的 onload 函数才能将焦点踢回原始页面以继续执行?

代码:

表格和文件上传:

<form id="attachForm" action="<%= Url.Action("TestUpload","Images") %>" 
      method="post"  enctype="multipart/form-data" target='upload_target' >
        <input id='actualupload' type='file' multiple="" name='file' />
</form>
<iframe id='upload_target' name='upload_target' src='' 
        style="width:0; height: 0; border: 0;" frameborder="0"></iframe>

jQuery 提交表单:

$("#actualupload").live('change',function()
{
    $("#attachForm").submit();
    alert("Fired!"); //This only occurs after clicking on the screen after the 
                     //file selection.
});
4

1 回答 1

3

似乎这.live()导致了这种奇怪的行为。如果你使用即

$(function() {
    $("#actualupload").change( function()
    {
        $("#attachForm").submit();
        alert("Fired!"); //This only occurs after clicking on the screen after the 
                         //file selection.
    });
});

它也适用于 IE。

于 2011-05-06T14:03:53.590 回答