简体   繁体   English

Bootstrap Twitter 输入类型文件如何清除文件?

[英]Bootstrap Twitter input type file how to clear the file?

I'm implementing option for the user in the form where they can select one of allowed files and upload.我正在以用户可以选择允许的文件之一并上传的形式为用户实施选项。 This feature interface is build in Bootstrap 3 and I found one of the blogs that helped me style the interface.此功能界面是在 Bootstrap 3 中构建的,我找到了帮助我设计界面样式的博客之一。 However, I'm wondering if there is a good option to clear/remove selected file?但是,我想知道是否有清除/删除所选文件的好方法? In case if user decided not to upload the file how they can remove it?如果用户决定不上传文件,他们如何删除它? I would need like X delete button on the very right side.我需要在最右侧的 X 删除按钮。 Also file has to be remove with JavaScript I guess.我猜还必须使用 JavaScript 删除文件。 If anyone knows the way to achieve this please let me know.如果有人知道实现这一目标的方法,请告诉我。 Here is my code example:这是我的代码示例:

 $("#frm_file").on("change", uploadFile); function uploadFile() { var ftype = $(this).get(0).files[0].type, fname = $(this).get(0).files[0].name, fextension = fname.split('.').pop(), // Another way to get file extension: fname.substring(fname.lastIndexOf('.')+1); validExtensions = ["jpg","jpeg","png","gif","doc","docx","xls","xlsx","txt","pdf"]; console.log(fname); $("#frm_filename").val(fname); }
 <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script language="javascript" type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="form-group"> <label class="control-label" for="file"><span class="label label-default">File:</span></label> <div class="input-group"> <label class="input-group-btn"> <span class="btn btn-primary"> Browse&hellip; <input type="file" name="frm_file" id="frm_file" style="display: none;"> </span> </label> <input type="text" class="form-control" name="frm_filename" id="frm_filename" readonly> </div> </div>

Try something like尝试类似的东西

document.getElementById('frm_file').value = "";

If not, try this answer.如果没有,请尝试答案。

Here is a very basic example using the bootstrap button on the right hand side.这是一个非常基本的示例,使用右侧的 bootstrap 按钮。 This is practically taken from the bootstrap 3.7 website.这实际上取自 bootstrap 3.7 网站。 https://getbootstrap.com/docs/3.3/css/#forms https://getbootstrap.com/docs/3.3/css/#forms

    <input type="text" class="form-control" id="inputGroupSuccess2" aria-describedby="inputGroupSuccess2Status">
  </div>
  <span id="clear" class="input-group-addon">X</span>
</div>

<script>
    Document.getElementById("clear").addEventListener("click", clearInput);

    Function clearInput(){
        Document.getElementById("clear").Value = "";
    }
</script>

In case you're banging your head against a brick wall like I've been, if you're using the .custom-file-input option from Bootstrap 4.x , none of the many and varied ways of clearing your file input will work.如果您像我一样用头撞砖墙,如果您使用Bootstrap 4.x 中.custom-file-input选项,则清除文件输入的多种方式都不会工作。 As the docs describe, this class causes Bootstrap to "hide the default file <input> via opacity and instead style the <label> ".正如文档所描述的,这个类会导致 Bootstrap “通过不透明度隐藏默认文件<input>并改为设置<label>样式”。 So to clear it, this is what I did:所以为了清除它,这就是我所做的:

Given (from the Bootstrap docs):鉴于(来自 Bootstrap 文档):

<div class="custom-file">
  <input type="file" class="custom-file-input" id="customFile">
  <label class="custom-file-label" for="customFile">Choose file</label>
</div>

Use:用:

var $input = $('#customFile')
$input.val(null);
$input.next('label').text('Choose file');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM