单选按钮 jQuery
我正在尝试显示一个 div,其中包含单选按钮选项 Yes
或 No
的输入字段。
HTML
<input id='companyreg' name='companyreg' class='radio' type='radio' value='yes' /> Yes
<input id='companyreg' name='companyreg' class='radio' type='radio' value='no' /> No
Jquery
var compfieldset = $("#companyfieldset");
//if javascript hide comp field
compfieldset.hide();
$("#companyreg").click(function(){
var value = $("input[@name=companyreg]:checked").val();
if (value === "yes") {
compfieldset.slideDown();
}
if (value != "yes") {
compfieldset.slideUp();
}
});
请你们帮帮我。
编辑
抱歉。
当我单击“否”时,什么也没有发生。
如果我单击“是”,则会显示字段集。
I'm trying to show a div with input fields of a radio button choice, Yes
or No
.
HTML
<input id='companyreg' name='companyreg' class='radio' type='radio' value='yes' /> Yes
<input id='companyreg' name='companyreg' class='radio' type='radio' value='no' /> No
The Jquery
var compfieldset = $("#companyfieldset");
//if javascript hide comp field
compfieldset.hide();
$("#companyreg").click(function(){
var value = $("input[@name=companyreg]:checked").val();
if (value === "yes") {
compfieldset.slideDown();
}
if (value != "yes") {
compfieldset.slideUp();
}
});
Please can you help me out guys.
EDIT
Sorry.
When I click on No, nothing happens.
If I click on yes, the fieldset shows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
同一文档中不能有两个具有相同 ID 的元素。您的选择器 #companyreg 正在查找第一个元素,而不是第二个元素。如果你要检查
你会看到 1.
你可能可以通过使用来克服这个问题,
尽管它不会修复你的无效文档。最好的方法是删除 ID 并向这些元素添加一个类,然后通过该类进行选择。
我还没有检查你的其余代码,看看这是否是唯一的问题,但我怀疑这是最大的问题。
You cannot have two elements in the same document with the same ID. Your selector, #companyreg, is finding the first element and not the second. If you were to check the
you'd see 1.
You could probably get past this by using
though it wouldn't fix your invalid document. The best way would be to remove the ID and add a class to those elements, then select via that class.
I have't checked the rest of your code to see if that's the only issue, but I suspect it's the biggest one.
ID 必须是唯一的。 使用类来代替:
事件处理程序可以简化为:
事件处理程序中不需要
:selected
选择器。与复选框不同,单击的单选按钮是选定的按钮。或者,您可以放弃
class
和id
并使用属性选择器:您还可以考虑使用
.delegate()
。无需将相同的事件处理程序绑定到两个元素。为什么它不能使用多个 ID?
ID 必须是唯一的,如果元素具有相同的 ID,则通过 ID 检索元素的行为未指定。但是,大多数浏览器返回出现在 DOM 中的第一个元素,这就是事件处理程序仅绑定到 yes 单选按钮的原因。
IDs have to be unique. Use classes instead:
and the event handler can be simplified to:
There is not need for the
:selected
selector in your event handler. Unlike for checkboxes, the clicked radio button is the selected button.Alternatively you can ditch
class
andid
and use an attribute selector:You could also consider to use event delegation using
.delegate()
. There is no need to bind the same event handler to both of the elements.Why does it not work with multiple IDs?
IDs have to be unique, the behaviour of retrieving elements by ID if they have the same ID is not specified. However, most browsers return the first element that appears at the DOM, that is why the event handler is only bound to the
yes
radio button.你的问题不够具体,无法知道问题是什么,但我可以指出一些问题:
if
如果只有两种状态,是和否,则不需要。此外,您是否有类似...
以及所有 ID 属性都需要是唯一的,如(见上文) id 属性)。
Your question is not specific enough to know what the problem is, but I can point out some issues:
if
you don't need if there are only two states, Yes and No.Also, do you have something like...
As well as all ID attributes need to be unique, as in (see above id attribute).