无法清除文件输入

时间:2019-01-17 09:48:49

标签: javascript jquery jasny-bootstrap

我正在使用Jasny Plugin,并试图清除通过输入上传的文件:

<div class="form-group">
    <div class="fileinput fileinput-new text-center" data-provides="fileinput">
        <div class="fileinput-new thumbnail" data-hasqtip="1" title="" aria-describedby="qtip-1">
            <img class="image-preview" src="../assets/img/image_placeholder.jpg" alt="...">
        </div>
        <div class="fileinput-preview fileinput-exists thumbnail" style=""></div>
        <div>
            <span class="btn btn-round btn-rose btn-file">
            <span class="fileinput-new">Add Photo</span>
            <span class="fileinput-exists">Change</span>
                <input type="hidden"><input type="file" name="..." id="product-image" accept="image/*">
            </span>
            <br>
            <a href="#" class="btn btn-danger btn-round fileinput-exists" data-dismiss="fileinput">
                <i class="fa fa-times"></i> 
                Remove </a>
        </div>
    </div>
</div>

上传图像后,预览显示在插件的框中,因此我按一个按钮即可执行以下代码:

$('#product-image').fileinput("clear");

但我收到此错误:

Cannot read property 'val' of undefined
    at c.clear (jasny-bootstrap.min.js:6)
    at HTMLInputElement.<anonymous> (jasny-bootstrap.min.js:6)
    at Function.each (jquery.min.js:2)
    at r.fn.init.each (jquery.min.js:2)
    at r.fn.init.a.fn.fileinput (jasny-bootstrap.min.js:6)
    at HTMLButtonElement.dispatch (jquery.min.js:3)
    at HTMLButtonElement.q.handle (jquery.min.js:3)

1 个答案:

答案 0 :(得分:1)

尝试改用:

$(".fileinput").fileinput("clear");

您不应该在插件中选择输入来清除它,而是整个fileinput类。