开发者问题收集

如何处理 jQuery 中的按钮点击事件?

2010-12-01
687345

我需要一个按钮并在 jQuery 中处理其事件。我正在编写此代码,但它不起作用。我错过了什么吗?

<!-- Begin Button -->  
<div class="demo">
<br> <br> <br>   
<input id = "btnSubmit" type="submit" value="Release"/>
<br> <br> <br>  
</div>
<!-- End Button -->

在 javascript 文件中

function btnClick()
{
    //    button click
    $("#btnSubmit").button().click(function(){
        alert("button");
    });    
}
3个回答

您必须将事件处理程序放在 $(document).ready() 事件中:

$(document).ready(function() {
    $("#btnSubmit").click(function(){
        alert("button");
    }); 
});
Davide Gualano
2010-12-01
$('#btnSubmit').click(function(){
    alert("button");
});

//Use this code if button is appended in the DOM
$(document).on('click','#btnSubmit',function(){
    alert("button");
});

有关更多信息,请参阅文档:
https://api.jquery.com/click/

Bruce Phillip Perez
2017-05-25
$(document).ready(function(){

     $('your selector').bind("click",function(){
            // your statements;
     });

     // you can use the above or the one shown below

     $('your selector').click(function(e){
         e.preventDefault();
         // your statements;
     });


});
Lawrence Gandhar
2014-04-28