简体   繁体   中英

How do I listen to click events on buttons with type submit using jQuery?

I have several buttons with type button:

<button type="button" class="btn btn-default">Plain old button.</button>

and some important buttons with type submit:

<button type="submit" class="btn btn-default">Submit button!</button>

I want to listen to click events on buttons with type submit, but not the others.

This can be done using CSS selectors:

$(function() {
    $('button[type=submit]').click(function () {
        alert('Submit button clicked.');
    });
});

W3C provides a CSS Selector Reference . All of which are supported by jQuery. The complete set jQuery selectors can be found at jQuery API . Here is a complete example at jsfiddle .

Try this,

$(function() { 
    $('button').click(function (e) { 
        if($(this).attr('type') =='submit') {
            alert($(this).attr('type'));
        }
    });
});

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM