简体   繁体   English

jQuery在复选框的更改和单击事件之间的区别

[英]jQuery difference between change and click event of checkbox

As title suggests I want to know the difference between the change and click event of checkbox (in jQuery) 标题建议我想知道复选框的changeclick事件之间的区别(在jQuery中)

I have read down the answer to this What the difference between .click and .change on a checkbox 我已经读过这个答案了.click和.change在复选框上有什么区别

But, its not working for me. 但是,它不适合我。 change fires even when I hit spaces without losing focus. 即使我在没有失去焦点的情况下击中空间也会change火焰

Here's a fiddle demo 这是一个小提琴演示

Both seems to be working alike. 两者似乎都是一样的。 Am I missing something here ? 我在这里错过了什么吗?

According to the W3C, the onclick event is triggered by the keyboard for accessibility purposes: 根据W3C, onclick事件由键盘触发以实现可访问性:

SCR35: Making actions keyboard accessible by using the onclick event of anchors and buttons SCR35:使用锚点和按钮的onclick事件使操作键盘可访问

In order to provide a better user experience for those without the use of a mouse, browsers have been developed to fire the onclick event even if the click occurs with a keyboard. 为了给那些不使用鼠标的人提供更好的用户体验,已经开发了浏览器来触发onclick事件,即使用键盘发生点击也是如此。

For this reason, jQuery's click event will fire even if the checkbox is clicked by using the keyboard's spacebar. 因此,即使使用键盘的空格键单击复选框,jQuery的click事件也会触发。 change , obviously, will fire every time the checkbox's state changes. change ,很明显,将触发每次复选框的状态变化。

The checkbox just happens to be the special case where change and click are interchangable, because you can't fire the change event without also triggering click . 该复选框恰好是changeclick可互换的特殊情况,因为您无法触发change事件而不会触发click

Of course, the exception to this rule is if you were to use javascript to manually alter the checkbox, such as: 当然,此规则的例外情况是,如果您使用javascript手动更改复选框,例如:

/* this would check the checkbox without firing either 'change' or 'click' */
$('#someCheckbox').prop('checked',true);

/* this would fire 'change', but not 'click'. Note, however, that this
   does not change the checkbox, as 'change()' is only the function that
   is fired when the checkbox changes, it is not the function that
   does the changing  */
$('#someCheckbox').trigger('change');

/* this would fire 'click', which by default change state of checkbox and automatically triggers 'change' */
$('#someCheckbox').trigger('click');

Here's a demonstration of these different actions: http://jsfiddle.net/jackwanders/MPTxk/1/ 以下是这些不同行为的演示: http//jsfiddle.net/jackwanders/MPTxk/1/

Hope this helps. 希望这可以帮助。

The main difference: when you click / hit space on a focused checkbox first the click event fired, with no changes. 主要区别在于:当您在聚焦复选框上单击/命中空间时,首先触发click事件,而不进行任何更改。 At this point you can still prevent the default action (with event.preventDefault() ) which would be to toggle the checked state of that checkbox, and fire the change event (which hasn't got default action). 此时,您仍然可以阻止默认操作(使用event.preventDefault() ),该操作将切换该复选框的已checked状态,并触发change事件(尚未执行默认操作)。

In some browsers the change event will fire only after the first blur after the click . 在某些浏览器中,更改事件仅在click后第一次blur后才会触发。

I would guess that change fires when the checkbox changes (even if you don't click on it). 我猜想当复选框发生变化时(即使你没有点击它),变化也会激发。 Eg if you change the checkbox's state via some other function. 例如,如果您通过其他功能更改复选框的状态。

The difference I experienced is this: 我遇到的不同之处是:

var $check = $(':checkbox');
$checkbox.checked = true;
$checkbox.click( function() {
    alert(this.checked); // true
}).change( function() {
    alert(this.checked); // false
});

So with click the state has not yet changed, with change it has... 所以点击状态尚未改变,改变它有......

you can check or uncheck checkbox without clicking, using keyboard (tab and space bars) this goes to change statement but not to click. 你可以选中或取消选中复选框而不点击,使用键盘(制表符和空格键)这会改变语句而不是点击。 IMHO 恕我直言

Please let me know if this doesn't help I will remove my post: 如果这没有帮助,请告诉我,我将删除我的帖子:

Change : http://api.jquery.com/change/ - I reckon change event get triggered when the state of the element if changed ie not clicked but changed. Changehttp//api.jquery.com/change/ - 我认为当元素的state发生更改(即未单击但已更改)时,将触发更改事件。 Checkbox has checked and unchecked state. 复选框已选中并取消选中状态。

Click : click event is when you click the element doesn't matter what the state of the checkbox is going to be. Click :单击事件是单击元素时无关紧要的复选框状态。

like in pseudo code 比如在伪代码中

if checkbox state is checked

    alert(Hulk is awesome);

if checkbox is click every time

    alert(Ironman is alright);

Change event fires when check box status has been changed through whatever occurrence will be happened either through pressing space when check box was focused Or Triggered change event Or clicked Where as Click event has been fired only when we clicked on check box through mouse click but at that time before click if change event has been specified then it also runs and then execute click event. 当复选框被聚焦时通过按空格键发生任何事件更改复选框状态时触发更改事件触发或触发更改事件或单击仅当我们通过单击鼠标单击复选框时触发了Click事件如果指定了更改事件,则在单击之前的那个时间,然后它也会运行,然后执行click事件。

Here you can see clear script has been done on bins: http://codebins.com/codes/home/4ldqpbu 在这里你可以看到关于垃圾箱的清晰脚本: http//codebins.com/codes/home/4ldqpbu

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

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