我有一個文件輸入元素從「選擇文件」窗口中選擇一個文件並關閉它後,如何調用JavaScript函數?
<input type="file" id="fileid">
如何調用從對話框窗口中選擇一個文件,並關閉它後的JavaScript函數?
我有一個文件輸入元素從「選擇文件」窗口中選擇一個文件並關閉它後,如何調用JavaScript函數?
<input type="file" id="fileid">
如何調用從對話框窗口中選擇一個文件,並關閉它後的JavaScript函數?
嘗試宣告你的文件的頂部:
<script type="text/javascript">
// this allows jquery to be called along with scriptaculous and YUI without any conflicts
// the only difference is all jquery functions should be called with $jQ instead of $
// e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
$jQ = jQuery.noConflict();
</script>
則:
<script language="javascript">
$jQ(document).ready(function(){
jQuery("input#fileid").change(function() {
alert(jQuery(this).val());
});
});
(你可以把兩者在同一<script>
標籤,但你可以例如,只需在你的父佈局中聲明第一部分,並在你的子佈局中使用jQuery時使用$ jQ,非常我們當與RoR工作時,例如)
正如在另一個答案中的評論中提到的,這將僅在選擇文件並單擊打開後觸發。如果您只需點擊「選擇文件」,並沒有任何選擇,它不會工作
<input type="file" id="fileid" >
當你把腳本輸入
<script type="text/javascript">
$(document).ready(function(){
$("#fileid").on('change',function(){
//do whatever you want
});
});
</script>
<script>
function example(){
alert('success');
}
</script>
<input type="file" id="field" onchange="example()" >
請添加一些額外的信息給你的答案。 – 2014-11-26 05:49:39
謝謝,這對我有用:) – 2015-02-12 02:55:42
我覺得下面的變化將發揮你的目的是可以實現的,但是AFAIK沒有這樣的事件。
但是爲了達到那個你需要配合3個事件, 我假設你只需要知道文件名。 我創建轉轉here
更動態的解決方案將是: jQuery的( 「輸入[類型=文件]」)變化(函數(){警報(jQuery的(本).VAL());});。 這將所有的文件裏幹活 – 2010-05-10 19:59:11