开发者

how to use jQuery to handle specific types of html controls

Creating a click event for an input is easy

$('input').click(function () {
                alert('adsf');
        });

But how can I be selective so that the previou开发者_如何学Pythons code only works on type checkbox or of type button, rather than using the generic 'input' selector?


You can use some built-in Selectors like :checkbox and :button to find these elements easily.

$('input:checkbox').click(function () {
    alert('checkbox');
});

$('input:button').click(function () {
    alert('button');
});

There's also :radio, :submit, :text, and :input selectors, among others


Check out this page in the jQuery documentation.


$('input[type=checkbox]').click(function(){});
$('input[type=button]').click(function(){});
//or
$('input:checkbox').click(function(){});
$('input:button').click(function(){});
//or combined, this is probably the best solution in your case
$('input:button, input:checkbox').click(function(){});

2 different approaches. Doesn't matter which one you use. Both to exactly the same. I think the best is to have both selectors together if possible in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜