IE使用.change()表单submit()

IE With on .change() form submit()

本文关键字:submit 表单 使用 IE change      更新时间:2023-09-26

下面是我的代码:

$('#button').click(function () {
    $('#file').click();
});
$('#file').change(function (){
    $('#form').submit();
});

 <form style="display:none;" id="form" method="post" target="upload_target" enctype="multipart/form-data" action="upload">
 <input id="file" name="file" id="file" type="file" /><br />        
  <input type="submit" name="action" value="Upload Image" /><br />      
  <iframe id="upload_target" name="upload_target" src="" style="width:100;height:100;border:00;"></iframe>   
</form>
  <span id="button" class="button def">upload</span>

问题在哪里?
这适用于Firefox, Chrome但不适用IE

检查你的选择器,像这样从第二部分的选择器中删除#。

$('input[type=file]').change(function(){
    $('#form').submit();
});

$('input[type=file]')应该是input只有当你想选择tagName,你应该使用#前选择器如果你想选择元素的ID

编辑:

如果你检查控制台,你会看到你在IE SCRIPT5: Access is denied.上有这个错误,这是因为IE的安全规则

你不能访问具有不同域名的页面中的Iframe。

经过一番搜索,我发现了一些可能对你有帮助的东西,你必须设置文档。域到父页面及其iframe上的相同内容,以便它们相互通信。

document.domain = "yourdomain.com"

来源:http://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/

好运。

浏览器不允许上传通过Javascript触发的文件。你正在做的,这是触发点击功能的文件浏览器通过一个按钮,然后试图发送所选择的任何文件将无法在IE中工作。您将得到访问拒绝错误。

这不是代码中的错误,它只是IE工作方式的限制。