How to Handle Button Click Events in jQuery?

前端 未结 9 1894
悲&欢浪女
悲&欢浪女 2020-12-04 08:04

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?

  
         


        
相关标签:
9条回答
  • 2020-12-04 08:27
    <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">
    
    0 讨论(0)
  • 2020-12-04 08:31

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

    $(document).ready(function() {
        $("#btnSubmit").click(function(){
            alert("button");
        }); 
    });
    
    0 讨论(0)
  • 2020-12-04 08:31

    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>
    
    0 讨论(0)
  • 2020-12-04 08:36
    $(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;
         });
    
    
    });
    
    0 讨论(0)
  • 2020-12-04 08:42
    $('#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/

    0 讨论(0)
  • 2020-12-04 08:42

    <script type="text/javascript">
    
        $(document).ready(function() {
    
        $("#Button1").click(function() {
    
            alert("hello");
    
        });
    
        }
        );
    
    </script>
    
    0 讨论(0)
提交回复
热议问题