最近在做一個圖片上傳的功能,出現提交一次后,file輸入框的change事件無法再次觸發的bug,就是說提交一次后必須刷新才能再次提交,這就坑了~
於是想辦法解決它~
在網上找了一些資料,找到這幾種方法:
1、替換掉原來的input框
2、remove原來的input框,然后在添加進新的一樣的input框
我測試了之后發現可以用下面的方法解決這個問題:
第一步:上傳完成后替換掉原來的input框
第二步:重新綁定onchange事件
問題解決!!
代碼如下:
1 <script> 2 $(document).ready(function () { 3 /* jquery handleError版本兼容 */ 4 jQuery.extend({ 5 handleError: function (s, xhr, status, e) { 6 if (s.error) { 7 s.error.call(s.context || s, xhr, status, e); 8 } 9 if (s.global) { 10 (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]); 11 } 12 }, 13 httpData: function (xhr, type, s) { 14 var ct = xhr.getResponseHeader("content-type"), 15 xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, 16 data = xml ? xhr.responseXML : xhr.responseText; 17 if (xml && data.documentElement.tagName == "parsererror") 18 throw "parsererror"; 19 if (s && s.dataFilter) 20 data = s.dataFilter(data, type); 21 if (typeof data === "string") { 22 if (type == "script") 23 jQuery.globalEval(data); 24 if (type == "json") 25 data = window["eval"]("(" + data + ")"); 26 } 27 return data; 28 } 29 }); 30 31 /* file輸入框變化時調用上傳圖片函數 */ 32 $(".myFile").change(function(){ 33 var objId = $.trim($(this).attr('id')); 34 myUpload(objId); 35 }); 36 /* 上傳函數 */ 37 function myUpload(objId) 38 { 39 var _obj = $('#'+objId); 40 var objVal = $.trim(_obj.val()); 41 if(!objVal){ 42 alert('你還未選擇圖片!'); 43 return false; 44 } 45 $.ajaxFileUpload({ 46 type: "post", 47 url: "upload.do", 48 secureuri:false, 49 fileElementId:objId, 50 dataType: "json", 51 success: function(result) { 52 if (result.code == "1") { 53 alert("上傳文件成功!"); 54 } 55 }, 56 complete: function(xmlHttpRequest) { 57 _obj.replaceWith('<input type="file" class="myFile" id="'+objId+'" name="'+objId+'" style="display:none;"/>'); 58 $("#"+objId).on("change", function(){ 59 myUpload(objId); 60 }); 61 }, 62 error: function(data, status, e) { 63 alert("文件上傳失敗!"); 64 } 65 }); 66 return false; 67 } 68 }); 69 </script>
代碼文本如下:
<script>
$(document).ready(function () {
/* jquery handleError版本兼容 */
jQuery.extend({
handleError: function (s, xhr, status, e) {
if (s.error) {
s.error.call(s.context || s, xhr, status, e);
}
if (s.global) {
(s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
}
},
httpData: function (xhr, type, s) {
var ct = xhr.getResponseHeader("content-type"),
xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
data = xml ? xhr.responseXML : xhr.responseText;
if (xml && data.documentElement.tagName == "parsererror")
throw "parsererror";
if (s && s.dataFilter)
data = s.dataFilter(data, type);
if (typeof data === "string") {
if (type == "script")
jQuery.globalEval(data);
if (type == "json")
data = window["eval"]("(" + data + ")");
}
return data;
}
});
/* file輸入框變化時調用上傳圖片函數 */
$(".myFile").change(function(){
var objId = $.trim($(this).attr('id'));
myUpload(objId);
});
/* 上傳函數 */
function myUpload(objId)
{
var _obj = $('#'+objId);
var objVal = $.trim(_obj.val());
if(!objVal){
alert('你還未選擇圖片!');
return false;
}
$.ajaxFileUpload({
type: "post",
url: "upload.do",
secureuri:false,
fileElementId:objId,
dataType: "json",
success: function(result) {
if (result.code == "1") {
alert("上傳文件成功!");
}
},
complete: function(xmlHttpRequest) {
_obj.replaceWith('<input type="file" class="myFile" id="'+objId+'" name="'+objId+'" style="display:none;"/>');
$("#"+objId).on("change", function(){
myUpload(objId);
});
},
error: function(data, status, e) {
alert("文件上傳失敗!");
}
});
return false;
}
});
</script>
done!