复选框 jquery 或 javascript oncheck?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7235957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checkbox jquery or javascript oncheck?
提问by cbr0wn
I do not know the correct terminology for this, but I want the same effect as onclick but for a check box with jquery or javascript.
我不知道正确的术语,但我想要与 onclick 相同的效果,但对于带有 jquery 或 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.
我想要与上面相同的效果,但需要一个复选框。最终结果将是页面应该使用更新的 php 查询重新加载,但我可以做到这一点。我只是不知道复选框的 onclick 是什么。
checkbox:
复选框:
<input type="checkbox" name="change" value="one" />changes php query
回答by Felix Kling
You should listen to the change
event, as the checkbox can be selected or deselect with the keyboard too:
您应该收听change
事件,因为也可以使用键盘选择或取消选择复选框:
$('input[type="checkbox"][name="change"]').change(function() {
if(this.checked) {
// do something when checked
}
});
Similarly with plain JavaScript:
与普通 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.
wherehandler
就像上面显示的处理程序。
Further reading:
进一步阅读:
回答by Burak Erdem
$('#input_id').click(function() {
// do what you want here...
});
回答by Evan
$('input:checked').click(function() {
//do something
});
See http://api.jquery.com/checked-selector/and http://api.jquery.com/checkbox-selector/
请参阅http://api.jquery.com/checked-selector/和http://api.jquery.com/checkbox-selector/