簡體   English   中英

用戶選擇文件時執行javascript函數

[英]execute a javascript function when user selects a file

在html頁面中,我正在添加輸入元素。

<input type="file" name="fileselect" id="fselect"> </input>

當用戶選擇文件時,我想執行javascript函數。假設我也要寫入控制台。

jquery select()是檢查用戶是否已選擇文件的正確函數嗎? 我嘗試過這樣

$(document).ready(function(){
   $('#fselect').select(function(){ 
      console.log('selected');
      myfun(); 
    });
});

function myfun(){
   alert('you selected a file');
}

出於某種原因,當我使用輸入元素選擇文件時,沒有任何內容寫入控制台,也沒有警告框出現。.select select()調用編碼是否錯誤?

您可以使用change事件:

​$("#fselect").change(function() {
    // do something
});​​​​​​​

演示: http : //jsfiddle.net/BtsHL/

您想要的事件是更改 ,而不是選擇

文檔

當用戶在元素內進行文本選擇時,會將選擇事件發送到該元素。

為了改變

更改事件在其值更改時發送到元素

嘗試這個:

$(document).ready(function(){
   $('#fselect').change(function(){ 
      console.log('selected');
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM