-2

How to find target is checkbox ?

$(document).click(function(e) {
    // Do things
});

how to find this ? if it's check box not need to proceed this events ?

4

5 回答 5

4

You can detect click on all elements (*) so you can use $(this).

$("*").on("click", function (e) {

    if ($(this).attr("type") === "checkbox") {
        alert("Checkbox click!");
    }
});

JSFIDDLE

If you want to detect clicks only on checkboxes use input[type='checkbox'] selector.

$("input[type='checkbox']").click(function(){
    ...
});

Like @XAOPT said we also can detect the clicks on document and then verifying if it's a checkbox using e.srcElement.

$(document).click(function(e) {    
    if(e.srcElement == "checkbox") { 
        alert("Checkbox click!");
    }
});

JSFIDDLE

于 2013-07-04T06:40:53.987 回答
2

或者

$(document).click(function(e) {    
    if(e.srcElement.type == 'checkbox') 
        alert('checkbox');
});
于 2013-07-04T06:50:23.813 回答
1
$(document).on('click', ':checkbox', function() {
    // do things
});

除非有理由您需要对文档进行通用单击并自己进行授权...

于 2013-07-04T06:43:28.463 回答
1

jQuery 选择器可以为您做到这一点:

$("input[type=checkbox]").click(function(){
    ...
});
于 2013-07-04T06:43:51.280 回答
0

你可以使用这个 $("input[type='checkbox']").click(function(){

});

于 2013-07-04T06:49:51.037 回答