[英]Execute a JavaScript function when user selects some text in an html file
[英]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.