单选按钮 jQuery

发布于 2024-10-18 00:42:15 字数 773 浏览 7 评论 0原文

我正在尝试显示一个 div,其中包含单选按钮选项 YesNo 的输入字段。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-10-25 00:42:15

同一文档中不能有两个具有相同 ID 的元素。您的选择器 #companyreg 正在查找第一个元素,而不是第二个元素。如果你要检查

$("#companyreg").length

你会看到 1.

你可能可以通过使用来克服这个问题,

$( '[id="companyreg"]' )

尽管它不会修复你的无效文档。最好的方法是删除 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

$("#companyreg").length

you'd see 1.

You could probably get past this by using

$( '[id="companyreg"]' )

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.

淑女气质 2024-10-25 00:42:15

ID 必须是唯一的。 使用类来代替:

<input class='companyreg radio' name='companyreg' type='radio' value='yes' /> Yes
<input class='companyreg radio' name='companyreg' type='radio' value='no' /> No

事件处理程序可以简化为:

$(".companyreg").click(function(){
    if (this.value === "yes") {
        compfieldset.slideDown();
    }
    else {
        compfieldset.slideUp();
    }
});

事件处理程序中不需要 :selected 选择器。与复选框不同,单击的单选按钮是选定的按钮。

或者,您可以放弃 classid 并使用属性选择器:

$('input[name=companyreg]')

您还可以考虑使用 .delegate()。无需将相同的事件处理程序绑定到两个元素。


为什么它不能使用多个 ID?

ID 必须是唯一的,如果元素具有相同的 ID,则通过 ID 检索元素的行为未指定。但是,大多数浏览器返回出现在 DOM 中的第一个元素,这就是事件处理程序仅绑定到 yes 单选按钮的原因。

IDs have to be unique. Use classes instead:

<input class='companyreg radio' name='companyreg' type='radio' value='yes' /> Yes
<input class='companyreg radio' name='companyreg' type='radio' value='no' /> No

and the event handler can be simplified to:

$(".companyreg").click(function(){
    if (this.value === "yes") {
        compfieldset.slideDown();
    }
    else {
        compfieldset.slideUp();
    }
});

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 and id and use an attribute selector:

$('input[name=companyreg]')

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.

遗失的美好 2024-10-25 00:42:15

你的问题不够具体,无法知道问题是什么,但我可以指出一些问题:

var compfieldset = $("#companyfieldset");
//if javascript hide comp field
compfieldset.hide();
$("#companyreg").click(function(){
    var value = $("input[@name=companyregyes]:checked").val();
    if (value == "yes") {
        compfieldset.slideDown();
    } else {
        compfieldset.slideUp();
    }
});
  1. 你不需要检查“yes”上的 === - 它总是会在此处键入 check the yes 作为字符串
  2. 第二个if 如果只有两种状态,是和否,则不需要。

此外,您是否有类似...

<div id="companyfieldset">
 <input id='companyregyes' name='companyreg' class='radio' type='radio' value='yes' /> Yes
 <input id='companyregno' name='companyreg' class='radio' type='radio' value='no' /> No
</div>

以及所有 ID 属性都需要是唯一的,如(见上文) id 属性)。

Your question is not specific enough to know what the problem is, but I can point out some issues:

var compfieldset = $("#companyfieldset");
//if javascript hide comp field
compfieldset.hide();
$("#companyreg").click(function(){
    var value = $("input[@name=companyregyes]:checked").val();
    if (value == "yes") {
        compfieldset.slideDown();
    } else {
        compfieldset.slideUp();
    }
});
  1. You don't need to check === on "yes" - it's always going to type check the yes as a string here
  2. The second if you don't need if there are only two states, Yes and No.

Also, do you have something like...

<div id="companyfieldset">
 <input id='companyregyes' name='companyreg' class='radio' type='radio' value='yes' /> Yes
 <input id='companyregno' name='companyreg' class='radio' type='radio' value='no' /> No
</div>

As well as all ID attributes need to be unique, as in (see above id attribute).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文