input-type=file onchange事件未在googlechrome/ffirefox中启动
input type=file onchange event is not firing in google chrome/firefox
更新:
已解决:http://jsfiddle.net/TmUmG/230/原问题:
我有一个图像/标志,旁边隐藏的输入类型=文件:
<form class="image fit" id="theuploadform">
<input title="click me to change logo" type="image" src="images/sample_image.jpg" style="width:inherit;height:inherit"/>
<input type="file" id="userfile" name="userfile" style="display:none" />
</form>
这个想法是点击图片浏览文件,上传并刷新原始图片。我的代码可以在IE中使用,但不能在Chrome/FF中使用(
点击处理:
// click for image upload
$("input[type='image']").click(function () {
$("input[id='userfile']").click();
});
这将提示我选择一个文件。一旦更改,我想提交表格:
// upload image form
$("input[id='userfile']").on("change", function() {
//alert('here!');
var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none"></iframe>');
$("body").append(iframe);
var form = $('#theuploadform');
form.attr("action", "/UploadHandler.ashx");
form.attr("method", "post");
form.attr("encoding", "multipart/form-data");
form.attr("enctype", "multipart/form-data");
form.attr("target", "postiframe");
form.attr("file", $('#userfile').val());
form.submit();
$("#postiframe").load(function () {
iframeContents = this.contentWindow.document.body.innerHTML;
$("#textarea").html(iframeContents);
var filename = $('#userfile').val().replace(/''/g, '/');
var fileNameIndex = filename.lastIndexOf("/") + 1;
filename = filename.substr(fileNameIndex);
$("input[type='image']").attr("src", "Uploads/" + filename);
});
//return false;
});
以上工作均在IE 9+中完成。
我刚刚发现这篇文章:使用输入类型="上传文件;文件";IE和Chrome中不总是触发带有.change((事件的字段还不确定,但听起来很有意义。
解决方案:对firefox使用"oninput"而不是"onchange"。
相关文章:
- WebRTC视频聊天可以在FF中使用,但不能在Chrome中使用
- FF视图源|脚本高亮显示为红色
- FF和Chromium中CSS样式按钮的外观差异
- jQuery Datepicker可以在Safari中工作,但不能在FF或Chrome中工作
- 在FF和IE中使用vimeo播放器的问题-加载flash播放器而不是使用HTML5播放器
- jQuery脚本在googlechrome中不起作用
- 如何修复FF和IE中的Javascript无效日期错误
- 使用一个Javascript函数提交多个表单可以在FF中工作,而不需要其他浏览器
- jQuery$.post可以在chrome、safari中工作,但不能在FF中工作(声明成功回调函数未定义)
- 可以'我没有集中精力在FF工作
- [AutoIt]如何使用FF.au3在FireFox上的页面中运行javascript
- Javascript/AAJAX在Opera中不起作用,在FF/IE/Chrome中完美工作
- 在FF和Chrome中接收用于JavaScript函数的TypeError
- 获取所选选项的Javascript在Webkit/FF中有效,但在IE中无效
- ng-show内容在页面加载过程中闪烁,尽管它不是真实的,并且ng-cloak不适用于FF
- FF和Chrome中的 ftp:// 网页不会加载 file:// 脚本
- Image Map 在 FF 和 IE 中不起作用(使用 onclick javascript)
- “google.maps.MapTypeId is undefined”在FF 14中使用GMAP3时
- HTML5 video .end 事件在 Chrome 中调用,但不是 FF
- jQuery .html('
') 在 FF 和 Opera 中不起作用