在用户选择要上传的文件后,我已修改当前表单以触发表单提交.上传完成后,我致电:
$('#file').val('');
Run Code Online (Sandbox Code Playgroud)
重置文件输入字段.
HTML
<input name="file" type="file" id="file"/>
Run Code Online (Sandbox Code Playgroud)
这适用于Chrome,FF和Safari,但在IE中不起作用.IE是否有解决方法或更好的解决方案?
一旦用户选择了文件,我就有条件使用html5文件api检查文件大小是否超过1mb.
function handleLimit(evt) {
var files = evt.target.files;
f = files[0];
console.log(f.size);
if (f.size > 1048576) {
/*JS for clearing form*/
}
}
Run Code Online (Sandbox Code Playgroud)
当处理程序知道文件超过一定大小时,我想在我的输入标签上运行以清除/强制客户端选择另一个文件的脚本是什么?
我在html页面中使用文件控件我想在用户点击重置按钮时从控件中删除文件位置.
$("#control").val("");
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码他不能在Chrome中工作但在Firefox中工作
我该怎么做才能取消选择未在文件上传控件中选择的文件.
如何重置控件而不重置整个表单.有任何事情可以做到这一点.
可能重复:
使用jQuery清空表单
我正在使用jQuery函数来清除我的所有表单值.
$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = '';
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
else if (tag == 'select')
this.selectedIndex = -1;
});
};
Run Code Online (Sandbox Code Playgroud)
上述功能的问题是它不会重置文件输入字段.无论如何,我可以扩展该功能,并使其与清除文件输入字段一起使用.
这是我正在使用的文件输入字段HTML代码.
<section><label for="productimg">Picture<br></label>
<div><input type="file" id="productimg" name="productimg"></div>
</section>
Run Code Online (Sandbox Code Playgroud)
谢谢..
我需要在页面加载时清除文件上传输入字段。我已经通过 stackoverflow 查看了 10-15 种不同的方法来执行此操作,但是每个建议都包含一个重置按钮,我想在页面加载时清除它,以便在用户单击浏览器上的后退按钮时重置/清除它。
我目前正在使用此代码来重置整个表单,它适用于所有下拉菜单,但它不会清除文件输入字段
function init() {
// Clear forms here
//reset inputs
$('#upload_form').trigger("reset");
//reset upload form
}
window.onload = init;
Run Code Online (Sandbox Code Playgroud)
根据我的研究,这似乎得到了来自社区Clearing <input type='file' /> using jQuery 的最多支持
干杯!
在 jquery 中,我试图清除一个字段,但如果我这样做,它会引发错误。它确实使该字段变空,但也会引发错误。我该如何解决?
谢谢
HTML
<input name="file" type="file" accept="image/*">
Run Code Online (Sandbox Code Playgroud)
JS
var input = $("#profile-edit-form").find("input[name=file]");
var fileName = input.val();
if(fileName) { // returns true if the string is not empty
input.replaceWith(input.val('').clone(true));
}
Run Code Online (Sandbox Code Playgroud)
错误
NotFoundError: Node was not found
-jquery.min.js:3:0
Run Code Online (Sandbox Code Playgroud) 如果用户正在选择文件,之后如果用户取消选中复选框,则文件也在那里,所以我想让它像用户取消选中复选框一样,而不是必须删除文件后.我该怎么做?哪个事件处理程序非常适合复选框?HTML代码:
<div class="checkbox">
<label for="email">electronics
<input type="checkbox" name="product_category[]" value="electronics" id="product_category" class="electronics">
</label>
</div>
<div class="form-group" id="efileu" style="display:none;" >
<input type="file" name="checkboxfile0" id="efile" style="width:100%">
</div>
<div class="checkbox">
<label for="email">kitchen
<input type="checkbox" name="product_category[]" value="kitchen" id="product_category" class="kitchen">
</div>
<div class="form-group" id="kfileu" style="display:none;" >
<input type="file" name="checkboxfile1" id="kfile" style="width:100%">
</div>
<script>
$(".electronics").click(function(){
if(!$("#efileu").is(":visible")){
$("#efileu").show();
}
else{
$("#efileu").hide();
$("#efileu").val();
}
});
$(".kitchen").click(function(){
if(!$("#kfileu").is(":visible")){
$("#kfileu").show();
}
else{
$("#kfileu").hide();
}
});
</script>
Run Code Online (Sandbox Code Playgroud)