jquery 在复选框上触发更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16055990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:17:47  来源:igfitidea点击:

jquery trigger change event on checkbox

javascriptjquery

提问by Zelter Ady

Is it possible to trigger change event on a checkbox using javascript/jquery?

是否可以使用 javascript/jquery 在复选框上触发更改事件?

Something like this (I run triggerChange on click of a button):

像这样(我点击一个按钮运行 triggerChange ):

<label><input type="checkbox" id="chk"/>Label for chk</label>

<script>
function triggerChange(){
    $("#chk").trigger("change");
}
</script>

When I run the above code I get this error: "trigger is not a function".

当我运行上面的代码时,我收到此错误:“触发器不是函数”。

采纳答案by Jace

That trigger is not a functionerror message indicates something else is at play. According to this SO question:

trigger is not a function错误消息表明还有其他事情在起作用。根据这个SO问题:

What happens when a jQuery selector wasn't found?

当找不到 jQuery 选择器时会发生什么?

no.good.at.codingsays:

no.good.at.coding说:

Do note however that you must ensure that selector is a jQuery object! Otherwise, you could get an error indicating that "trigger is not a function".

但是请注意,您必须确保选择器是一个 jQuery 对象!否则,您可能会收到一条错误消息,指出“触发器不是函数”。

It's likely that you have forgotten jQuery?

您可能已经忘记了 jQuery?



As for your implementation, you should be fine the way you are using it. But triggershould be used to trigger event methods on elements that have already been attached via jQuery. Check out my demo:

至于您的实现,您应该以正确的方式使用它。但trigger应该用于触发已经通过 jQuery 附加的元素上的事件方法。看看我的演示:

Fiddle:
With click event: http://jsfiddle.net/fS4R5/1/
Without click event: http://jsfiddle.net/fS4R5/2/

小提琴:
有点击事件:http: //jsfiddle.net/fS4R5/1/
没有点击事件:http: //jsfiddle.net/fS4R5/2/

HTML:

HTML:

<label><input type="checkbox" id="chk"/>Label for chk</label>

JS:

JS:

function triggerChange(){
    $("#chk").trigger("change");
}

$("#chk").change(function() {
   alert("triggered!"); 
});

triggerChange();

回答by Joachim VR

In jQuery, you can usually trigger an event by calling it's eventhandler method withoud any function parameters.

在 jQuery 中,您通常可以通过调用它的 eventhandler 方法而不带任何函数参数来触发事件。

For example a click handler can be assigned as such:

例如,点击处理程序可以这样分配:

$('#mything').click(function(e){dostuff});

$('#mything').click(function(e){dostuff});

the click event in itself can be triggered by simply running: $('#mything').click();

单击事件本身可以通过简单地运行来触发: $('#mything').click();

I suspect this can be done for every existing event in jQuery.

我怀疑这可以为 jQuery 中的每个现有事件完成。

回答by Yeronimo

I think the preferred method since 1.9.1 is 'on'. Specially if you use dynamically added checkboxes.

我认为自 1.9.1 以来的首选方法是“开”。特别是如果您使用动态添加的复选框。

Say you have a div with id='divCOntent' and on it is a checkbox with id='cballaut', you could do this

假设你有一个带有 id='divCONtent' 的 div,上面有一个带有 id='cballaut' 的复选框,你可以这样做

$('#divcontent').on('click', '#cballaut', function (e) {
                    alert(this.checked);
                });

回答by Alex

Be sure that Input of Type checkbox is enabled, in case is disabled trigger will not fire event

确保启用输入类型复选框,如果禁用触发器将不会触发事件

//fire event 
$('#ceckBoxId').click();
$('#ceckBoxId').trigger('click');

or change checkbox checkedval

或更改复选框checkedval

$('#ceckBoxId').prop('checked', true);
$('#ceckBoxId').prop('checked', false);

回答by Nishad Up

use prop method.

使用道具方法。

$('#myCheck').prop('checked', true);
$('#myCheck').prop('checked', false);