是否可以<input type='file' />使用jQuery 清除控件值?我尝试过以下方法:
$('#control').attr({ value: '' });
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
如果我使用$('表单输入')在IE11中清除包含5个或更多字段的表单.val("")IE11将崩溃.HTML:
<form>
<label>1</label><input type="text"/>
<label>2</label><input type="text"/>
<label>3</label><input type="text"/>
<label>4</label><input type="text"/>
<label>5</label><input type="text"/>
</form>
Run Code Online (Sandbox Code Playgroud)
JS:
$(document).ready(function(){
$('#clearFormNormal').click(function(){
$("form input").val("");
});
});
Run Code Online (Sandbox Code Playgroud)
当我这样做递归并使用setTimeout时,它可以工作.
JS:
function clearFields (counter) {
var i = counter || 0, deferred = new $.Deferred();
if ($("form input").eq(i).length === 1){
setTimeout(function(){
$("form input").eq(i).val("");
i = i + 1;
clearFields(i).always(function(){
deferred.resolve();
});
},0);
} else {
deferred.resolve();
}
return deferred.promise();
}
$(document).ready(function(){
$('#clearFormSetTimeout').click(function(){
clearFields();
});
});
Run Code Online (Sandbox Code Playgroud)
见http://jsfiddle.net/fransoverbeek/Cy5D5/7/以及
这是IE11的错误吗?
这是我在chrome中注意到的奇怪之处.如果用户选择一个文件然后再次选择同一个文件再次打开文件对话框,则chrome不会在firefox执行时触发onchange事件.
有人注意到了吗?