开发者

checkbox jquery or javascript oncheck?

I do not know the correct terminology for this, but I want the same effect as onclick but f开发者_Go百科or a check box with jquery or javascript.

onclick version:

<a href="..." onclick="function()">Link</a>

I want the same effect as above but for a checkbox. The end result will be that the page should reload with an updated php query, but that part I can do. I just don't know what the onclick is for checkboxes.

checkbox:

<input type="checkbox" name="change" value="one" />changes php query


You should listen to the change event, as the checkbox can be selected or deselect with the keyboard too:

$('input[type="checkbox"][name="change"]').change(function() {
     if(this.checked) {
         // do something when checked
     }
 });

Similarly with plain JavaScript:

// checkbox is a reference to the element

checkbox.onchange = function() {
     if(this.checked) {
         // do something when checked
     }
};

And last, although you really should not use inline event handlers, but if you have to:

<input ... onchange="handler.call(this)" />

where handler is like the handlers shown above.


Further reading:

  • jQuery documentation
  • MDN JavaScript Guide
  • quriksmode.org Introduction to Events


$('#input_id').click(function() {
    // do what you want here...
});


$('input:checked').click(function() {
   //do something
});

See http://api.jquery.com/checked-selector/ and http://api.jquery.com/checkbox-selector/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜