使用jQuery清除控件的值是否可能?我已经尝试了以下方法:
$('#control').attr({ value: '' });
但是它没有起作用。
使用jQuery清除控件的值是否可能?我已经尝试了以下方法:
$('#control').attr({ value: '' });
但是它没有起作用。
<!-- Html Markup --->
<input id="btn" type="file" value="Button" onchange="function()" />
<script>
//Function
function function(e) {
//input your coding here
//Reset
var controlInput = $("#btn");
controlInput.replaceWith(controlInput = controlInput.val('').clone(true));
}
</script>
这适用于Chrome、FF和Safari
$("#control").val("")
可能不兼容IE或Opera浏览器
对我来说它不起作用:
$('#Attachment').replaceWith($(this).clone());
or
$('#Attachment').replaceWith($('#Attachment').clone());
所以在ASP MVC中,我使用Razor功能来替换文件输入。 首先创建一个带有ID和名称的输入字符串变量,然后在页面上使用它并在重置按钮单击时进行替换:
@{
var attachmentInput = Html.TextBoxFor(c => c.Attachment, new { type = "file" });
}
@attachmentInput
<button type="button" onclick="$('#@(Html.IdFor(p => p.Attachment))').replaceWith('@(attachmentInput)');">--</button>
var input = $('#attachments');
input.prop('type', 'text');
input.prop('type', 'file')
您可以这样用克隆替换它
var clone = $('#control').clone();
$('#control').replacewith(clone);
但是这个克隆也会带着它的值,所以你最好这样做。
var emtyValue = $('#control').val('');
var clone = emptyValue.clone();
$('#control').replacewith(clone);
这很容易哈哈(适用于所有浏览器[除了Opera]):
$('input[type=file]').each(function(){
$(this).after($(this).clone(true)).remove();
});
JS Fiddle: http://jsfiddle.net/cw84x/1/
JS Fiddle:http://jsfiddle.net/cw84x/1/document.onlyform.upload.value="";
假设上传是名称:
<input type="file" name="upload" id="csv_doc"/>
我正在使用JSP,不确定这是否有所不同...
对我来说有效,而且我认为它更容易。