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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4323848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:04:38  来源:igfitidea点击:

How to Handle Button Click Events in jQuery?

jquery

提问by Ahmad Farid

I need to have a button and handle its event in jQuery. And I am writing this code but it'snot working. Did I miss something?

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

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

And in the javascript file

在 javascript 文件中

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

回答by Davide Gualano

You have to put the event handler in the $(document).ready() event:

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

$(document).ready(function() {
    $("#btnSubmit").click(function(){
        alert("button");
    }); 
});

回答by Lawrence Gandhar

$(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;
     });


});

回答by Bruce Phillip Perez

$('#btnSubmit').click(function(){
    alert("button");
});

or

或者

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

See documentation for more information:
https://api.jquery.com/click/

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

回答by Ali Tarhini

 $("#btnSubmit").click(function(){
        alert("button");
    });    

回答by Rafiqul Islam

Try This:

尝试这个:

$(document).on('click', '#btnClick', function(){ 
    alert("button is clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <button id="btnClick">Click me</button> 

回答by Nivediny

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
  $("#button").click(function(){
    alert("Hello");
  });
});
</script>

<input type="button" id="button" value="Click me">

回答by Hiren

$('#btnSubmit').click(function(event){
    alert("Button Clicked");
});

or as you are using submit button so you can write your code in form's validate event like

或者当您使用提交按钮时,您可以在表单的验证事件中编写代码,例如

$('#myForm').validate(function(){
    alert("Hello World!!");
});

回答by Anup Ghanshala

<script type="text/javascript">

    $(document).ready(function() {

    $("#Button1").click(function() {

        alert("hello");

    });

    }
    );

</script>

回答by navyad

Works for me

为我工作

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$("#btn").click(function() {
    alert("email")
});

</script>