JQuery:在单选按钮选择上显示 div

发布于 2024-08-29 01:07:45 字数 724 浏览 3 评论 0原文

当用户选择单选按钮组中的特定单选按钮(Other)时,我尝试使用 jQuery 显示 div。

html 如下

<div id="countries">

<input id="Washington_D.C" type="radio" name="location" value="Washington">Washington D.C</input>
<input id="Other" type="radio" name="location" value="">Other</input>
    <div id="other locations" style="display: none">
    </div>
</div>

使用 JQuery 代码:

$(document).ready(function(){
    $("radio[@name='location']").change(function(){
        if ($("radio[@name='location']:checked").val() == 'Other')
        $("#county_drop_down").show();
    });
 });

但是当我选择单选按钮 Other 时,它没有显示 div 其他位置。

I am trying to use jQuery to show a div when the user selects a particular radio button (Other) within a radiobutton group.

The html is below

<div id="countries">

<input id="Washington_D.C" type="radio" name="location" value="Washington">Washington D.C</input>
<input id="Other" type="radio" name="location" value="">Other</input>
    <div id="other locations" style="display: none">
    </div>
</div>

Using the JQuery code:

$(document).ready(function(){
    $("radio[@name='location']").change(function(){
        if ($("radio[@name='location']:checked").val() == 'Other')
        $("#county_drop_down").show();
    });
 });

But it's not showing the div other locations when I select the radiobutton Other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

热风软妹 2024-09-05 01:07:45

您需要为其指定值Other

因此,不是这样,

<input id="Other" TYPE="RADIO" NAME="location" VALUE="">Other</input>

而是这样

<input id="Other" TYPE="RADIO" NAME="location" VALUE="Other">Other</input>

乍一看,您的 jQuery 代码看起来不错。然而,我更喜欢click而不是change,因为如果没有预选的方法,change不会在第一次点击时被触发。

You need to give it a value of Other.

Thus, not so

<input id="Other" TYPE="RADIO" NAME="location" VALUE="">Other</input>

but so

<input id="Other" TYPE="RADIO" NAME="location" VALUE="Other">Other</input>

At first glance, your jQuery code looks fine. I would however prefer click above change, since change doesn't get fired on 1st click if there's no means of preselection.

眼前雾蒙蒙 2024-09-05 01:07:45

尝试将

$("radio[name='location']").change(function(){
    if ($(this).val() === 'Other')
    {
        if($(this).is(":checked"))
        {
            $("#county_drop_down").show();
        }
    }
});

单选按钮值设置为“其他”以使此代码正常工作。

Try

$("radio[name='location']").change(function(){
    if ($(this).val() === 'Other')
    {
        if($(this).is(":checked"))
        {
            $("#county_drop_down").show();
        }
    }
});

set the radio button value to Other for this code to work.

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