简体   繁体   English

如何显示/隐藏一个 <div> 当单选按钮被点击时 <div> 有同一个班级?

[英]How can I show/hide one <div> when a radio button is clicked on, when all the <div>s have the same class?

I have this JavaScript code: 我有以下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();
    }
});

I also have a <form> with different radio buttons. 我也有一个带有不同单选按钮的<form> When I click on a radio button, the .subvragen <div> must be shown. 当我单击单选按钮时,必须显示.subvragen <div>

But every .radio button has its own .subvragen <div> . 但是每个.radio按钮都有其自己的.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? 如何更改此JavaScript代码,以便在单击该单选按钮时仅显示每个单选按钮中的.subvragen

When I click on a radio button now, all the .subvragen <div> s are shown. 现在单击单选按钮时,将显示所有.subvragen <div>

Edit: 编辑:
I have uploaded the files. 我已经上传了文件。 You can see them here: http://mikevierwind.nl/karel/offerterelatie.html 您可以在这里看到它们: http : //mikevierwind.nl/karel/offerterelatie.html

Looking at the HTML you've got , you could use the following JavaScript (I think): 查看您拥有的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();
    }
});

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. 另外,如果您可以将id属性添加到subvragen <div>和收音机,则可以使用它们-这将使JavaScript减少对HTML嵌套的依赖。

Eg if your HTML were like this: 例如,如果您的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>

Then your JavaScript would go something like this: 然后,您的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();
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript单击单选按钮时隐藏DIV - hide DIV when radio button is clicked with Javascript 当我有多个具有相同类名的div时如何显示/隐藏某些div - How to show/hide certain div when i have multiple div with same class name 选中单选按钮时显示 div,并隐藏所有其他 div? - Show div when radio button is checked, and hide all the other divs? Javascript - 单击时显示DIV内的按钮(并隐藏所有其他按钮) - Javascript - show a button inside of a DIV when clicked (and hide all others) 在 React 中单击单选按钮时如何显示 div - How to Show div when a Radio Button is Clicked in React 当单击按钮时,显示/隐藏div将打开所有div,而不仅仅是一个 - show/hide div is opening all divs when button is clicked instead of just one 如何创建一个单击事件,其中我有多个 div,并且每个 div 都有一个具有相同 Id 的按钮,但单击时仅影响父 div,如何? - How can I create a click event where I have several div's and each having a button with the same Id but when clicked only affect the parent div, how? 单击按钮时如何显示 div 内容并隐藏其他内容 - JavaScript - How to show div content and hide others when button is clicked - JavaScript 显示div onclick并在其外部单击时隐藏div并关闭按钮 - show a div onclick and Hide div when clicked outside of it and close button 如何隐藏页面上除一个 div 之外的所有内容,然后在单击按钮后显示它? - How can I hide everything on a page except for one div, and then show it once a button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM