2012-10-07 40 views
0

我試圖收集和前面加上所選擇的輸入文件的文件名到表:顯示<INPUT TYPE = 「文件」>文件名

HTML

<div class="row-fluid"> 
    <div class="span12" id="upload-modal-button"> 
    <span class="btn btn-small btn-success fileinput-button"> 
     <span>Browse</span> 
     <input type="file" name="file_1" enctype="multipart/form-data" /> 
    </span> 
    </div> 
</div> 

<div class="row-fluid" id="image-table"> 
    <div class="span12"> 
    <table class="table table-striped" id="upload-list"> 
     <thead> 
     <td>Filename</td> 
     <td>State</td> 
     <td>Rate</td> 
     </thead> 
     <tr id="filelist"></tr> 
    </table> 
    </div> 
</div> 

JQUERY

$('input[type=file]').change(function(e){ 
    $('#filelist').prepend('<td>'+$(this).val()+'</td>'); 
}); 

但我不能得到文件名顯示在表中。

+1

[這個簡化版本似乎工作。](http://jsfiddle.net/H7kaD/) – lonesomeday

+0

[工作這裏](http://jsbin.com/enocib/1/edit)。 –

+0

[Works fine](http://jsfiddle.net/H7kaD/1/)適用於IE9,FF13和Chrome22。 –

回答

1

如果您使用'jquery 1.8',選擇器'type = file'可以正常工作,但如果您使用'jquery 1.6',則此選擇器將無法工作。 正確的方法是使用這種類型的選擇的:

$('input[type="file"]') 

,或者您可以使用此類型的選擇,用類:

HTML:

<input class="input-type-file" type="file" name="file_1" enctype="multipart/form-data" /> 

JS:

$('.input-type-file') 
+0

這是文件輸入特有的問題嗎? – tomaroo

+0

@tomaroo我不知道,對我來說正確的方法是包括引號,是以前的版本有雙引號或單引號的問題。可以改進現在能夠排除引號 –

相關問題