简体   繁体   English

使用javascript选择后如何更改文件输入值?

[英]How to change the file input value after selected using javascript?

<div class="form-group">
<span class='btn btn-file btn-block  btn-default'> <i class='pe pe-7s-video'> </i> Choose from Library
<input type='file'  class='form-control' name='answer_video' accept='video/*' capture='camera' placeholder='Record' id='answer_video'></span>
</div>

How to change the value "Choose from Library" to "video selected" after choosing file through file input using javascript 通过使用javascript通过文件输入选择文件后,如何将值“从库中选择”更改为“已选择视频”

you may take help with jquery, hope it will help you. 您可能会在jquery方面寻求帮助,希望它将对您有所帮助。

$("input[type='file'][name='answer_video']").change(function(){
    var id = $(this).attr("id");
    var thisVal = document.getElementById(id).files[0].name;
    var nameBox = '<div class="fileContainer"><span class="docFileName">'+thisVal+'</span><span class="glyphicon glyphicon-remove" onclick=removeAttachment("'+id+'")>X</span></div>';
    $(this).before(nameBox).hide();
});

function removeAttachment(id){
    $("#"+id).val("").show();
    $(".fileContainer").remove();
}

On change event, check if the this.files are more than 0 . change事件时,请检查this.files是否大于0

Demo 演示版

 document.querySelector( "[name='answer_video']" ).addEventListener( "change", function(){ if ( this.files.length > 0 ) { this.parentNode.querySelector(".videoLabel").innerHTML = "file selected"; } else { this.parentNode.querySelector(".videoLabel").innerHTML = "Choose from Library"; } }); 
 <div class="form-group"> <span class='btn btn-file btn-block btn-default'> <i class='pe pe-7s-video'> </i> <span class="videoLabel">Choose from Library</span> <input type='file' class='form-control' name='answer_video' accept='video/*' capture='camera' placeholder='Record' id='answer_video'></span> </div> 

You can use EventListener DOMContentLoaded and onchange do something reference here 您可以使用EventListener DOMContentLoadedonchange 在这里做一些参考

 document.addEventListener('DOMContentLoaded',function() { document.querySelector('[type="file"]').onchange=changeEventHandler; },false); function changeEventHandler(event) { // You can use “this” to refer to the selected element. document.getElementById('video_option').innerHTML = 'video selected'; } 
 <div class="form-group"> <span class='btn btn-file btn-block btn-default'> <i class='pe pe-7s-video'> </i><span id='video_option'> Choose from Library</span> <input type='file' class='form-control' name='answer_video' accept='video/*' capture='camera' placeholder='Record' id='answer_video'></span> </div> 

You only need to use the onchange function with your input . 您只需要在input使用onchange函数。

 function changeName(){ document.getElementById("change").innerHTML = "changeName" } 
 <div class="form-group"> <span id="change" class='btn btn-file btn-block btn-default'> <i class='pe pe-7s-video'> </i> Choose from Library </span> <input type='file' class='form-control' name='answer_video' placeholder='Record' id='answer_video' onchange="changeName()"> </div> 

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

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