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

时间:2016-01-14 01:04:19

标签: javascript php

是否可以使用文件上传中选择的文件名填充文本框?

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

这是我的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=""/>

1 个答案:

答案 0 :(得分:0)

这是我的更新:

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

我刚刚在输入文件中添加了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=""/>