这是我的代码:
<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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句