为什么jQuery .change()没有开始单选按钮被取消选择作为一个名称被选中的结果? [英] Why does jQuery .change() not fire on radio buttons deselected as a result of a namesake being selected?

查看:128
本文介绍了为什么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/

推荐答案

如果您想到一组单选按钮,例如选择元素,变得清楚为什么没有必要为取消选择的单选按钮启动更改事件。

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.

当您更改所选择的选项时,请选择元素,您会收到一个更改事件(您不会期望任何其他事件),而不是以前选择的选项,另一个为新选择的opti

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天全站免登陆