简体   繁体   English

从文件选择中填充文本字段

[英]Populate text field from file select

Is it possible to fill the text box with the file name that is selected in file upload? 是否可以使用在文件上传中选择的文件名填充文本框?

<script type="text/javascript">
var textbox = document.getElementById('fiil');
var fileselect = document.getElementById('uploadData');

fileselect.onchange = function(){
      textbox.value = textbox.value  + this.value; //to appened
     //textbox.innerHTML = this.value; 
}

here is my html code: 这是我的html代码:

<form  id="upload" name="upload" action="<?php echo base_url(); ?>index.php/welcome/upload_gen048" method="POST" class="form-horizontal" enctype="multipart/form-data" onsubmit="return verify_upload()" >
<div class="col-lg-4"></div>
<b style=" padding:.3em; font-size:15px;color:white"> </b><input style="border:1px solid" type="text" onfocus="true"  name="fiil" id="fiil" value=""size="30" />
<input type="file" name="uploadData" id="uploadData" style=""/>

Here is my update: 这是我的更新:

<script type="text/javascript">
var textbox = document.getElementById('fiil');
var fileselect = document.getElementById('uploadData');

function fileSelect()
{
    var x =  document.getElementById('uploadData').value;
    var fileName = x.match(/[^\/\\]+$/);

    document.getElementById('fiil').value = fileName;
}

I just added onChange="fileSelect() in the input file here: 我刚刚在输入文件中添加了onChange="fileSelect()

<form  id="upload" name="upload" action="<?php echo base_url(); ?>index.php/welcome/upload_gen048" method="POST" class="form-horizontal" enctype="multipart/form-data" onsubmit="return verify_upload()" >

<div class="col-lg-4"></div>
<b style=" padding:.3em; font-size:15px;color:white"> </b>
<input style="border:1px solid" type="text" onfocus="true"  name="fiil" id="fiil" value=""size="30" />

<input type="file" name="uploadData" id="uploadData" onChange="fileSelect();" style=""/>

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

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