如何显示/隐藏一个
当单击单选按钮时,当所有
具有相同的类时?

发布于 2024-09-29 22:26:24 字数 938 浏览 0 评论 0原文

我有这个 JavaScript 代码:

$("#midden-offertes form .subvragen").hide();

$("#midden-offertes form .veld .radio :radio").change(function() 
{
    if (this.value === 'Ja')
    { 
        $("#midden-offertes form .subvragen",this).show()
    }
    else
    {
        $("#midden-offertes form .subvragen",this).hide();
    }
});

我还有一个带有不同单选按钮的

。当我单击单选按钮时,必须显示 .subvragen

但每个 .radio 按钮都有自己的 .subvragen

。如何更改此 JavaScript 代码,以便当我单击每个单选按钮时仅显示每个单选按钮中的 .subvragen

现在,当我单击单选按钮时,将显示所有 .subvragen

编辑:
我已经上传了文件。您可以在这里看到它们:http://mikevierwind.nl/karel/offerterelatie.html

I have this JavaScript code:

$("#midden-offertes form .subvragen").hide();

$("#midden-offertes form .veld .radio :radio").change(function() 
{
    if (this.value === 'Ja')
    { 
        $("#midden-offertes form .subvragen",this).show()
    }
    else
    {
        $("#midden-offertes form .subvragen",this).hide();
    }
});

I also have a <form> with different radio buttons. When I click on a radio button, the .subvragen <div> must be shown.

But every .radio button has its own .subvragen <div>. How can I change this JavaScript code so that only the .subvragen within each radio button is shown when I click on that radio button?

When I click on a radio button now, all the .subvragen <div>s are shown.

Edit:
I have uploaded the files. You can see them here: http://mikevierwind.nl/karel/offerterelatie.html

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

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

发布评论

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

评论(1

七月上 2024-10-06 22:26:24

查看您获得的 HTML,您可以使用以下 JavaScript(我认为)

$("#midden-offertes form .veld .radio :radio").change(function() 
{
    var $subvragen = $(this).parents('.veld').siblings('.subvragen');

    if (this.value === 'Ja')
    { 
        $subvragen.show();
    }
    else
    {
        $subvragen.hide();
    }
});

: ,如果您可以将 id 属性添加到您的 subvragen

和收音机中,您就可以使用它们 - 它会使 JavaScript更少依赖 HTML 嵌套。

例如,如果你的 HTML 是这样的:

<div id="midden-offertes">
    <form>
        <div class="veld">
            <div class="radio">
                <input name="geoff" id="geoff_1" value="a" />
            </div>
        </div>
        <div class="subvragen" id="subvragen_geoff_1"></div>

        <div class="veld">
            <div class="radio">
                <input name="geoff" id="geoff_2" value="b" />
            </div>
        </div>
        <div class="subvragen" id="subvragen_geoff_2"></div>

        <div class="veld">
            <div class="radio">
                <input name="geoff" id="geoff_3" value="c" />
            </div>
        </div>
        <div class="subvragen" id="subvragen_geoff_3"></div>
    </form>
</div>

那么你的 JavaScript 就会像这样:

$("#midden-offertes form .subvragen").hide();

$("#midden-offertes form .veld .radio :radio").change(function() 
{
    if (this.value === 'Ja')
    { 
        $(".subvragen#subvragen_" + this.id).show()
    }
    else
    {
        $(".subvragen#subvragen_" + this.id).hide();
    }
});

Looking at the HTML you’ve got, you could use the following JavaScript (I think):

$("#midden-offertes form .veld .radio :radio").change(function() 
{
    var $subvragen = $(this).parents('.veld').siblings('.subvragen');

    if (this.value === 'Ja')
    { 
        $subvragen.show();
    }
    else
    {
        $subvragen.hide();
    }
});

Alternatively, if you could add id attributes to your subvragen <div>s and radios, you could use them — it’d make the JavaScript less reliant on the HTML nesting.

E.g. if your HTML were like this:

<div id="midden-offertes">
    <form>
        <div class="veld">
            <div class="radio">
                <input name="geoff" id="geoff_1" value="a" />
            </div>
        </div>
        <div class="subvragen" id="subvragen_geoff_1"></div>

        <div class="veld">
            <div class="radio">
                <input name="geoff" id="geoff_2" value="b" />
            </div>
        </div>
        <div class="subvragen" id="subvragen_geoff_2"></div>

        <div class="veld">
            <div class="radio">
                <input name="geoff" id="geoff_3" value="c" />
            </div>
        </div>
        <div class="subvragen" id="subvragen_geoff_3"></div>
    </form>
</div>

Then your JavaScript would go something like this:

$("#midden-offertes form .subvragen").hide();

$("#midden-offertes form .veld .radio :radio").change(function() 
{
    if (this.value === 'Ja')
    { 
        $(".subvragen#subvragen_" + this.id).show()
    }
    else
    {
        $(".subvragen#subvragen_" + this.id).hide();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文