Javascript input type=file onchange事件未在google chrome/firefox中触发
更新:已解决:Javascript input type=file onchange事件未在google chrome/firefox中触发,javascript,jquery,html,google-chrome,Javascript,Jquery,Html,Google Chrome,更新:已解决: 原始问题: 我在隐藏的输入类型=文件旁边有一个图像/徽标: <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" i
原始问题: 我在隐藏的输入类型=文件旁边有一个图像/徽标:
<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>
这会提示我选择一个文件。一旦更改,我想提交以下表格:
// 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;
});
//上传图像表单
$(“input[id='userfile'])。在(“change”,function(){
//警惕(“这里!”);
变量iframe=$('');
$(“正文”)。附加(iframe);
变量形式=$(“#上传形式”);
attr(“action”,“/UploadHandler.ashx”);
表格attr(“方法”、“职位”);
attr(“编码”、“多部分/表单数据”);
form.attr(“enctype”、“多部分/表单数据”);
表格attr(“目标”、“后框架”);
form.attr(“file”,$('#userfile').val();
表单提交();
$(“#positframe”).load(函数(){
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/”+文件名);
});
//返回false;
});
所有这些都在IE9+中工作
我刚找到这篇帖子:
还不确定,但听起来很相关。解决方案:对firefox使用“oninput”而不是“onchange”。
$(“输入[id='userfile'])。单击()代码>而您的另一个事件是更改
?请使用相同的事件作为form.attr(“文件“,$(“#用户文件”).val()的预期结果
文件
对象位于表单
属性文件
?在$(“input[type='image']”)attr(“src”,“Uploads/”+文件名)处的`上载/“
的目的是什么?'代码>?我有一个处理程序:UploadHandler.ashx,它读取表单,将文件保存在服务器上的“Uploads”文件夹中。我正在引用我正在使用的方法保存的图像的预期路径,如下所述:
// 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;
});