我有一个文件上传方法,它有添加,删除,清除字段,添加和删除工作完美。在清除方法中,我有两个文件输入值,当我点击清除时,两个输入值都被清除了。我想只清除特定文件的输入值。
如何解决这个问题
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
$('.multi-field .clear-field', $wrapper).click(function() {
//if ($('.multi-field', $wrapper).length > 1)
if(navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0){
$("input[type='file']").replaceWith($("input[type='file']").clone(true));
} else {
$("input[type='file']").val('');
}
});
});
<div class="multi-field-wrapper">
<div class="multi-fields">
<div class="multi-field">
<input type="file" value="choose files" id="attach" name="attach[]" onchange="check_file_sizes();"/>
<button type="button" class="remove-field">Remove</button>
<button type="button" class="clear-field">Clear</button>
<!--<button type="button" id="clear" >Clear</button> -->
</div>
</div>
<button type="button" class="add-field">Add field</button>
</div>
问题出在你的选择器上。检查这个 小提琴. 你应该改变你的 clear
职能如下:
$('.multi-field .clear-field', $wrapper).click(function() {
//if ($('.multi-field', $wrapper).length > 1)
if(navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0){
$(this).closest('.multi-field').find("input[type='file']").replaceWith($("input[type='file']").clone(true));
} else {
$(this).closest('.multi-field').find("input[type='file']").val('');
}
});