如何清除文件输入

下面是我的部分jquery代码,显示文件输入和一个"Clear File"按钮。

var $imagefile = $('<input />').attr({
    type: 'file',
    name: 'imageFile',
    class: 'imageFile'
});

$image.append($imagefile);

var $imageclear = $('<input />').attr({
    type: 'button',
    name: 'imageClear',
    class: 'imageClear',
    value: 'Clear File'
});

$image.append($imageclear);

现在,我之所以有"清除文件"按钮,是因为如果你点击该按钮,那么它将清除文件输入中的任何内容。我如何编写代码,使其在我点击"清除文件"按钮时真正清除文件输入?

解决办法

这应该是可行的:

$imageClear.on('click', function() { 
    $imageFile.val(''); 
});
评论(5)

这也是我喜欢使用的方法,但我相信你需要在克隆方法中加入bool true参数,以使任何事件保持与新对象相连,并且你需要清除内容。

    var input = $("#fileInput");

    function clearInput() {
        input = input.val('').clone(true);
    };

https://api.jquery.com/clone/

评论(0)

这样做的目的是

 $("#yourINPUTfile").val("");
评论(0)