如何使用 jQuery 或 JavaScript 从 <input type='file' /> 中清除文件
How to clear files from <input type='file' /> using jQuery or JavaScript
我的文件中有几个 <input type='file' />
字段,但没有任何 html form
。我想清除特定 <input type='file' />
中的附加文件。并非来自所有领域。我使用了 $('input').val("");
但它清除了所有 <input type='file' />
字段。那么我如何清除特定 <input type='file' />
字段中的附件?
var control = $("#mcontrol");
$("#clear").on("click", function() {
$('input').val("");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="mcontrol" /><br>
<input type="file" id="mcontrol2" />
<button id="clear">Clear</button>
这里是fiddle
您可以使用您已经设置的id
:
var control = $("#mcontrol");
$("#clear").on("click", function() {
control.val("");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="mcontrol" />
<br>
<input type="file" id="mcontrol2" />
<button id="clear">Clear</button>
使用 JavaScript 你可以按如下方式进行。
document.getElementById("#control").value = "";
// For first file feild
$("#clear").on("click", function () {
$('#mcontrol1').val("");
});
// For second file feild
$("#clear").on("click", function () {
$('#mcontrol2').val("");
});
我的文件中有几个 <input type='file' />
字段,但没有任何 html form
。我想清除特定 <input type='file' />
中的附加文件。并非来自所有领域。我使用了 $('input').val("");
但它清除了所有 <input type='file' />
字段。那么我如何清除特定 <input type='file' />
字段中的附件?
var control = $("#mcontrol");
$("#clear").on("click", function() {
$('input').val("");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="mcontrol" /><br>
<input type="file" id="mcontrol2" />
<button id="clear">Clear</button>
这里是fiddle
您可以使用您已经设置的id
:
var control = $("#mcontrol");
$("#clear").on("click", function() {
control.val("");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="mcontrol" />
<br>
<input type="file" id="mcontrol2" />
<button id="clear">Clear</button>
使用 JavaScript 你可以按如下方式进行。
document.getElementById("#control").value = "";
// For first file feild
$("#clear").on("click", function () {
$('#mcontrol1').val("");
});
// For second file feild
$("#clear").on("click", function () {
$('#mcontrol2').val("");
});