Jquery 如何设置按钮将文件输入恢复为空白 函数insertQuestion(form){ 变量$tbody=$(“#qanadbl>tbody”); var$tr=$(“”); 变量$image=$(“”); $weight.append($weightText); }); var$imagefile=$(“”) 艾特先生({ 键入:“文件”, 名称:'imageFile', 类:“imageFile” }); $image.append($imagefile); 变量$imageclear=$('') 艾特先生({ 键入:“按钮”, 名称:“imageClear”, 类:“imageClear”, 值:“清除文件” }); $tr.append($image); $tbody.append($tr); }

Jquery 如何设置按钮将文件输入恢复为空白 函数insertQuestion(form){ 变量$tbody=$(“#qanadbl>tbody”); var$tr=$(“”); 变量$image=$(“”); $weight.append($weightText); }); var$imagefile=$(“”) 艾特先生({ 键入:“文件”, 名称:'imageFile', 类:“imageFile” }); $image.append($imagefile); 变量$imageclear=$('') 艾特先生({ 键入:“按钮”, 名称:“imageClear”, 类:“imageClear”, 值:“清除文件” }); $tr.append($image); $tbody.append($tr); },jquery,Jquery,在上面的代码中,我有一个文件输入和一个按钮。我的问题是,我希望设置按钮,以便当用户单击按钮时,它会从文件输入中删除用户已选择的文件url,以便文件输入可以返回为空,我如何才能做到这一点?将单击事件绑定到按钮,这实际上用一个新的元素替换了文件输入元素 function insertQuestion(form) { var $tbody = $('#qandatbl > tbody'); var $tr = $("<tr class='optionAndAnsw

在上面的代码中,我有一个文件输入和一个按钮。我的问题是,我希望设置按钮,以便当用户单击按钮时,它会从文件输入中删除用户已选择的文件url,以便文件输入可以返回为空,我如何才能做到这一点?

单击
事件
绑定到
按钮
,这实际上用一个新的元素替换了文件输入元素

function insertQuestion(form) {   

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
 var $image = $("<td class='image'></td>");


 $weight.append($weightText);

        });

                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'
            });

$tr.append($image);
$tbody.append($tr); 

}
$(“”)
艾特先生({
键入:“按钮”,
名称:“imageClear”,
类:“imageClear”,
值:“清除文件”
})。单击(函数(){
$('#fileInputId')。替换为('';
});

编辑:对不起,我点击提交太早了

将文件输入包装在div或span中,并使用容器的HTML重置该容器的HTML。我更喜欢这种方法,因为它更通用(可以放在插件中),并且不需要我在JavaScript中重新指定输入HTML


重置
$(函数(){
$(“按钮”)。单击(函数(){
$('.inputFile wrap').html($('.inputFile wrap').html());
});
});

这应该满足您的要求:

<span class="inputFile-wrap"><input type="file" class="inputFile" /></span>

<button type="button">Reset</button>

$(function() {
    $('button').click(function() {
        $('.inputFile-wrap').html($('.inputFile-wrap').html());
    });
});

为什么要尝试用同一文件元素的内容替换文件元素的内容?这不会重置file元素的值,您必须替换它。我还没有完成;提交答案太早。
<span class="inputFile-wrap"><input type="file" class="inputFile" /></span>

<button type="button">Reset</button>

$(function() {
    $('button').click(function() {
        $('.inputFile-wrap').html($('.inputFile-wrap').html());
    });
});
$('.imageClear').click(function() {
   $('.imageFile').val('');
});