2016-03-14 228 views
1

我需要趕在jQuery的單擊事件,這是我的HTML代碼:如何處理點擊按鈕的jQuery

<input type="button" id="aggiungiDomande" value="+"> 

,這是我的jQuery代碼:

$('#aggiungiDomande').on('click',function(){ 
alert("HI"); 
}); 

我想我wroing因爲我什麼也沒有顯示。任何人都可以幫助我?

+0

它工作正常。如果你沒有把代碼放在'$(document).ready(function(){// code}'看到https://jsfiddle.net/harishkommuri/yb4yp29z/ – Harish

+0

你把你的javascript代碼包裝到'$(文件)。就緒()'? – reporter

回答

0

你可以嘗試這樣的事:

$(document).on('click', '#aggiungiDomande',function(){ 
    alert("HI"); 
}); 

如果在執行你的代碼你的按鈕不存在,任何情況下,西港島線得到綁定到它。你可以在這裏閱讀更多關於事件綁定:http://api.jquery.com/on/

1
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 


<script> 
$("document").ready(function(){ 
$('#aggiungiDomande').on('click',function(){ 
alert("HI"); 
}); 
}); 

</script> 
</head> 
<body> 

<button id = "aggiungiDomande">Click me</button><br><br> 

</body> 
</html>