jQuery和多个单选按钮组问题 [英] JQuery and multiple radio button groups problem

查看:65
本文介绍了jQuery和多个单选按钮组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JQuery处理多个单选按钮组时遇到麻烦.出于某种原因,JQuery代码无法分辨两组单选按钮之间的区别,也无法分辨我单击了哪一组单选按钮.

I am having trouble using JQuery to work with multiple radio button groups. The JQuery code, for some reason, cannot tell the difference between the two groups of radio buttons and cannot tell which group of radio button I clicked on.

这是HTML代码:

<!-- Radio button group 1 -->
<input type="radio" name="group_1" value="full_day_ticket"/>
<input type="radio" name="group_1" value="half_day_ticket"/>

<!-- Radio button group 2 -->
<label><input type="radio" name="group_2" value="boarder"/> Snowboard</label>
<label><input type="radio" name="group_2" value="skier"/> Ski</label>

JQuery代码如下:

And the JQuery code is as follows:

$("input:radio[@name='group_2']").click(function() {
  alert('group 2 clicked');
}

现在,当我单击名为"group_1"的单选按钮时,JQuery认为我单击了"group_2"中的单选按钮并显示警报窗口.出于某种原因,JQuery似乎无法识别@ name ='group_2'过滤器,无法捕获页面上所有单选按钮的点击,而不仅仅是捕获名为'group_2'的单选按钮.

Now when I click on the radio buttons named 'group_1', JQuery thinks that I clicked on a radio button in 'group_2' and displays the alert window. For some reason, it seems that JQuery is not recognising the @name='group_2' filter and captures clicks on all radio buttons on the page, instead of just the radio buttons named 'group_2'.

有人以前遇到过这个问题吗?还是我在做一些愚蠢的事情?

Has anyone come across this problem before? Or am I doing something stupid?

谢谢!

推荐答案

一个小的更改将解决此问题:

A small change will fix it:

$("input:radio[name=group_2]").click(function() {
  alert('group 2 clicked');
});

这篇关于jQuery和多个单选按钮组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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