这是我的代码:
<html>
<head>
<style type="text/css">
#myform {display:none;}
</style>
</head>
<body>
<button type="button" id="uploadbutton">Upload Images</button>
<form name="myform" id="myform" method="POST" action="http://www.########.com/post.php" enctype="multipart/form-data">
<input type="file" name="imagefile" id="imagefile" accept="image/*" onchange="FileSelected();" />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$('#uploadbutton').click(function() {
$('#imagefile').trigger('click');
});
function FileSelected() {
document.getElementById("myform").submit();
}
</script>
</body>
</html>
我试图让它在 IE9 和更旧的浏览器中工作,但是 jQuery submit() 函数没有触发。最终,我正在构建一个脚本,该脚本可以在不刷新页面的情况下提交图像并且隐藏表单。一个复杂的 FileSelected() 函数在现代浏览器上通过 AJAX 提交文件图像,尽管我需要帮助获得替代方案以在不支持此 AJAX/文件提交的旧浏览器上工作。
让我们从简单的开始:为什么上面的这个小脚本不能在 IE9 上运行?
最佳答案
发生这种情况是因为您没有提交表单。
代替:
$('#imagefile').trigger('click');
你需要使用:
$('#myform').submit();
我的片段:
$(function () {
$('#imagefile').on('change', function (e) {
if (this.value != '') {
alert('loaded');
// uncomment for testing
//$('#myform').submit();
}
});
$('#uploadbutton').click(function () {
$('#imagefile').trigger('click');
});
});
#myform {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<button type="button" id="uploadbutton">Upload Images</button>
<form name="myform" id="myform" method="POST" action="http://www.########.com/post.php" enctype="multipart/form-data">
<input type="file" name="imagefile" id="imagefile" accept="image/*"/>
</form>
关于javascript - 使用 jQuery 提交简单表单 - 为什么这在 IE9 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211390/