how to detect which button is clicked using jQuery

前端 未结 4 1096
青春惊慌失措
青春惊慌失措 2020-12-17 10:46

how to detect which button is clicked using jQuery

相关标签:
4条回答
  • 2020-12-17 11:05
    $(function() {
        $('input[type="button"]').click(function() { alert('You clicked button with ID:' + this.id); });
    });
    
    0 讨论(0)
  • 2020-12-17 11:06
    $("input").click(function(e){
        var idClicked = e.target.id;
    });
    
    0 讨论(0)
  • 2020-12-17 11:10

    Since the block is added dynamically you could try:

    jQuery( document).delegate( "#dCalc input[type='button']", "click",
        function(e){
        var inputId = this.id;
        console.log( inputId );
        }
    );
    

    demo http://jsfiddle.net/yDNWc/

    0 讨论(0)
  • 2020-12-17 11:20

    jQuery can be bound to an individual input/button, or to all of the buttons in your form. Once a button is clicked, it will return the object of that button clicked. From there you can check attributes such as value...

    $('#dCalc input[type="button"]').click(function(e) {
        // 'this' Returns the button clicked:
        // <input id="btn1" type="button" value="Add">
        // You can bling this to get the jQuery object of the button clicked
        // e.g.: $(this).attr('id'); to get the ID: #btn1
        console.log(this);
    
        // Returns the click event object of the button clicked.
        console.log(e);
    });
    
    0 讨论(0)
提交回复
热议问题