Javascript 即使通过Javascript代码检查复选框,如何触发复选框单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4780912/
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
How to trigger checkbox click event even if it's checked through Javascript code?
提问by Jishnu A P
I have many checkboxes in my page and there is a select all checkbox which checks all the checkboxes. Somehow I want to emulate that click event of checkbox even if it's checked/unchecked through select all button. How can I do it?
我的页面中有很多复选框,并且有一个全选复选框可以检查所有复选框。不知何故,我想模拟复选框的点击事件,即使它通过全选按钮被选中/取消选中。我该怎么做?
回答by Mark Robinson
You can use the jQuery .trigger()
method. See http://api.jquery.com/trigger/
您可以使用 jQuery.trigger()
方法。见http://api.jquery.com/trigger/
E.g.:
例如:
$('#foo').trigger('click');
回答by Harish
Getting check status
获取检查状态
var checked = $("#selectall").is(":checked");
Then for setting
然后进行设置
$("input:checkbox").attr("checked",checked);
回答by silviomoreto
You can use .change()
function too
你也可以使用.change()
函数
E.g.:
例如:
$('form input[type=checkbox]').change(function() { console.log('hello') });
回答by Harijs Krūtainis
no gQuery
没有 gQuery
document.getElementById('your_box').onclick();
document.getElementById('your_box').onclick();
I used certain class on my checkboxes.
我在复选框上使用了某些类。
var x = document.getElementsByClassName("box_class");
var i;
for (i = 0; i < x.length; i++) {
if(x[i].checked) x[i].checked = false;
else x[i].checked = true;
x[i].onclick();
}
回答by Learning and sharing
You can also use this, I hope you can serve them.
你也可以用这个,希望你能为他们服务。
$(function(){
$('#elements input[type="checkbox"]').prop("checked", true).trigger("change");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="elements">
<input type="checkbox" id="item-1" value="1"> Item 1 <br />
<input type="checkbox" id="item-2" value="2" disabled> Item 2 <br />
<input type="checkbox" id="item-3" value="3" disabled> Item 3 <br />
<input type="checkbox" id="item-4" value="4" disabled> Item 4 <br />
<input type="checkbox" id="item-5" value="5"> Item 5
</div>
回答by Learning and sharing
Trigger function from jQuery could be your answer.
来自 jQuery 的触发函数可能就是你的答案。
jQuery docs says:Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user
jQuery 文档说:当相应的事件发生时,任何附加有 .on() 或其快捷方法之一的事件处理程序都会被触发。但是,它们可以使用 .trigger() 方法手动触发。调用 .trigger() 以与用户自然触发事件相同的顺序执行处理程序
Thus best one line solution should be:
因此最好的单行解决方案应该是:
$('.selector_class').trigger('click');
//or
$('#foo').click();
回答by Raj Shekhar
$("#gst_show>input").change(function(){
var checked = $(this).is(":checked");
if($("#gst_show>input:checkbox").attr("checked",checked)){
alert('Checked Successfully');
}
});