为什么 jQuery .change() 不会在由于选择同名而取消选择的单选按钮上触发? [英] Why does jQuery .change() not fire on radio buttons deselected as a result of a namesake being selected?

查看:25
本文介绍了为什么 jQuery .change() 不会在由于选择同名而取消选择的单选按钮上触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有多个同名的单选按钮,则任何时候只能选择一个.当一个人被选中时,任何同名的人都会失去他们的选择.我很好奇为什么这不构成 jQuery 的 .change() 事件.

If I have several radio buttons of the same name, only one can be selected at any one time. When one becomes selected, any namesakes lose their selection. I'm intrigued as to why this doesn't constitute a .change() event for jQuery.

破解 $.change() 函数会非常激烈,但这似乎没有被报告为错误 - 所以我有兴趣找出为什么这不是案例.

Hacking the $.change() function would be pretty drastic, but this doesn't seem to be reported as a bug — so I'm interested in finding out why this isn't the case.

下面的测试:我希望在选择收音机时触发两个更改事件,但事实并非如此.

Test below: I'd expect the two change events to fire whenever a radio is selected, but that isn't the case.

示例如下:http://jsfiddle.net/XzmmW/

推荐答案

如果您想到一组单选按钮,例如 select 元素,就很清楚为什么不需要为取消选中的单选按钮.

If you think of a set of radio buttons like a select element, it becomes clear why it's unnecessary to fire a change event for the deselected radio button.

当您更改 select 元素中的选定选项时,您会收到一个更改事件(您不会期待其他任何事情),而不是先前选择的选项的一个,以及新选择的另一个事件选择的选项.

When you change the selected option in a select element, you get one change event (you wouldn't expect anything else), rather than one for the previously selected option, and another for the newly selected option.

正如已经提到的,当一个单选按钮被选中时,同一组中的其他单选按钮都不能被选中,因此触发第二个更改事件是多余的.

As has already been mentioned, when one radio button is selected, no others in the same set can be, so it would be redundant to fire a second change event.

另请注意,这并非特定于 jQuery - 标准 JavaScript 更改事件的工作方式完全相同.

Also note that this is not specific to jQuery - the standard JavaScript change event works in exactly the same way.

这篇关于为什么 jQuery .change() 不会在由于选择同名而取消选择的单选按钮上触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆