简体   繁体   English

jQuery 检查复选框并触发 javascript onclick 事件

[英]jQuery checking a checkbox and triggering javascript onclick event

I'm trying to check a checkbox using jQuery and trigger the onclick event in the process.我正在尝试使用 jQuery 检查复选框并在此过程中触发 onclick 事件。

Say I have a checkbox defined in html:假设我在 html 中定义了一个复选框:

<input type="checkbox" value="checked" name="check1" onclick="alert(this.value);">

And I have a jQuery statement that is triggered in a function:我有一个在函数中触发的 jQuery 语句:

$('input[name=check1]').attr('checked', true);

The result is the checkbox gets checked but the javascript onclick event is not triggered (hence no alert).结果是复选框被选中,但未触发 javascript onclick 事件(因此没有警报)。 But if I were to trigger the click even manually as such:但是,如果我要手动触发点击:

$('input[name=check1]').attr('checked', true).trigger('click');

The result is the checkbox gets checked, javascript onclick event is triggered (and the value is correctly gotten) but then the checkbox gets unchecked after that.结果是复选框被选中,javascript onclick 事件被触发(并且值被正确获取),但之后复选框被取消选中。

Can anyone tell me how I can achieve what I'm trying to do?谁能告诉我如何实现我想要做的事情?

Use .triggerHandler() instead of .trigger() :使用.triggerHandler()而不是.trigger()

$('input[name=check1]').attr('checked', true).triggerHandler('click');

Also, use .prop() instead of .attr() :另外,使用.prop()而不是.attr()

$('input[name=check1]').prop('checked', true).triggerHandler('click');

(if you're using jQuery 1.6 or newer.) edit — Also, as I commented on another answer, you have to watch out for jQuery's weird behavior when programmatically triggering events. (如果您使用的是 jQuery 1.6 或更高版本。)编辑— 此外,正如我在另一个答案中评论的那样,您必须在以编程方式触发事件时注意 jQuery 的怪异行为。 Here is an illustrative jsfiddle.这是一个说明性的 jsfiddle。 When a real "click" happens on an element, the "click" handler for the element will see the updated value of the "checked" flag.当一个真正的“点击”发生在一个元素上时,该元素的“点击”处理程序将看到“已检查”标志的更新值。 That is, if you click on an unchecked checkbox, the click handler will see the "checked" flag set to true .也就是说,如果您单击未选中的复选框,则单击处理程序将看到“已选中”标志设置为true However, if you trigger "click" on an unchecked checkbox via jQuery, the "click" handler will see the "checked" flag set to false !但是,如果您通过 jQuery 在未选中的复选框上触发“单击”,“单击”处理程序将看到“已选中”标志设置为false That's a really bad thing, in my opinion, but it's always done that.在我看来,这是一件非常糟糕的事情,但它总是那样做。

edit again — oh, also, I forgot another important (and irritating) jQuery weirdness: for reasons unknown, the .triggerHandler() API will only invoke handlers on the first matched element .再次编辑- 哦,还有,我忘记了另一个重要(且令人恼火)的 jQuery .triggerHandler() :出于未知的原因, .triggerHandler() API 只会在第一个匹配的元素上调用处理程序。 If you try to trigger all the checkbox "click" handlers, in other words:如果您尝试触发所有复选框“单击”处理程序,换句话说:

$('input:checkbox').triggerHandler('click');

then only the first checkbox on the page will be triggered.那么只会触发页面上的第一个复选框。 What I generally do in order to deal with the insanity is bind the handler with my own fake event name as well as "click":为了处理这种疯狂,我通常做的是将处理程序与我自己的假事件名称以及“单击”绑定:

$('input:checkbox').bind('click my-click', function() ... ) // or ".on()" with 1.7

That way I can trigger "my-click" and get the best of both worlds: the library triggers the handler on all the matched elements, but it won't toggle the actual state of the elements.这样我就可以触发“我的点击”并获得两全其美:库在所有匹配的元素上触发处理程序,但它不会切换元素的实际状态。

Change:改变:

$('input[name=check1]').attr('checked', true).trigger('click');

To:到:

$('input[name=check1]').trigger('click').prop('checked', true);

this answer seems to work fine:这个答案似乎工作正常:

$('input[name=check1]').attr('checked', true).change();

jsFiddle demo jsFiddle 演示

.on('changed', ...) is definitely what you should be binding to on checkbox elements (it behaves much more reasonably. Then you could just manually trigger a .click() on the elements you want to toggle the state of to see the proper event trigger happen (and see the DOM in a matching state to what those handlers also saw). .on('changed', ...)绝对是你应该在复选框元素上绑定的(它的行为更合理。然后你可以手动触发你想要切换状态的元素上的.click()查看正确的事件触发器发生(并查看处于匹配状态的 DOM 与那些处理程序也看到的内容)。

<input type="checkbox" value="checked" name="check1">
<script>
    $('input').on('change', function(e) {alert(e.target.value);});
    $('input').click();
    // see the alert, and if you checked $(e.target).is(':checked') you should get true
</script>

on click checked..again click uncheck..单击选中..再次单击取消选中..
Here is the logic of this这是这个的逻辑

$('input[name=check1]').prop('checked',!$('input[name=check1]').prop('checked'));

Just an addon to the other posts since I cannot comment.只是其他帖子的插件,因为我无法发表评论。 I have found that I had to use a dictionary for the prop thing.我发现我必须使用字典来表示道具。

IE IE

$('input[name=check1]').prop({'checked' : !$('input[name=check1]'}).prop('checked'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM