简体   繁体   English

Javascript显示隐藏两个单选按钮的公共Div

[英]Javascript Show Hide common Div for two Radio Button

Guys this is little different from what answers are in internet. 伙计们这与互联网上的答案有什么不同。 Here I have 3 radio button and 2 division which are primarily hidden. 这里我有3个单选按钮和2个分区,主要是隐藏的。 One division (say div1) is for one radio button (say Radio1) and another division (say div2) is for other two radio button (say Radio2 & Radio3). 一个分区(比如div1)用于一个单选按钮(比如Radio1),另一个分区(比如div2)用于另外两个单选按钮(比如Radio2和Radio3)。

Below is my HTML code. 下面是我的HTML代码。

<div class="form-group">
     <label class="radio-inline">
        <input type="radio" onclick="javascript:radioCheck();" name="optionsRadiosInline" id="directradio" value="option1">Direct
     </label>

     <label class="radio-inline">
        <input type="radio" onclick="javascript:radioCheck();" name="optionsRadiosInline" id="chequeradio" value="option2">Cheque
      </label>

      <label class="radio-inline">
        <input type="radio" onclick="javascript:radioCheck();" name="optionsRadiosInline" id="cashradio" value="option3">Cash
      </label>
</div>

Division 1 第1分部

<div class="col-md-12" id="showbank" style="display:none;">
    <div class="form-group">
         <label class="col-sm-4 control-label">Comment </label>
              <div class="col-sm-6">
                   <textarea type="text" rows="3" class="form-control" name="comment" id="comment"></textarea>
              </div>
    </div>
</div>

Division 2 第2分部

<div class="col-md-12" id="showcashbox" style="display:none;">
    <div class="form-group">
         <label class="col-sm-4 control-label">Comment </label>
              <div class="col-sm-6">
                   <textarea type="text" rows="3" class="form-control" name="comment" id="comment"></textarea>
              </div>
    </div>
</div>

JavaScript Code Below. 下面的JavaScript代码。

function radioCheck() {
if (document.getElementById('directradio').checked) {
    document.getElementById('showbank').style.display = 'block';
}
else {
    document.getElementById('showbank').style.display = 'none';
}

if (document.getElementById('chequeradio').checked) {
    document.getElementById('showcashbox').style.display = 'block';
}
else {
    document.getElementById('showcashbox').style.display = 'none';
}

if (document.getElementById('cashradio').checked) {
    document.getElementById('showcashbox').style.display = 'block';
}
else {
    document.getElementById('showcashbox').style.display = 'none';
}
}

In the above case Radio 1 and Radio 3 button is working fine. 在上面的例子中,Radio 1和Radio 3按钮工作正常。 But when I click Radio 2 button the required division2 is not coming. 但是当我单击Radio 2按钮时,所需的division2不会出现。 Can anyone help what is the minor problem in this. 任何人都可以帮助解决这个问题。

If the second div is to be displayed for either of the two check boxes, then you need to combine the condition check like so: 如果要为两个复选框中的任何一个显示第二个div,则需要组合条件检查,如下所示:

function radioCheck() 
 {
    if (document.getElementById('directradio').checked) {
        document.getElementById('showbank').style.display = 'block';
    }
    else {
        document.getElementById('showbank').style.display = 'none';
    }

    if (document.getElementById('chequeradio').checked || document.getElementById('cashradio').checked) {
        document.getElementById('showcashbox').style.display = 'block';
    }
    else {
        document.getElementById('showcashbox').style.display = 'none';
    }
}

If we execute your code, it will show the the div only if the third checkbox is clicked, if its not the else part of the third checkbox is the final code that gets executed on second div click as well. 如果我们执行你的代码,它只会在点击第三个复选框时显示div,如果它不是第三个复选框的else部分是在第二个div点击时执行的最终代码。

Try the following code. 请尝试以下代码。

You need to use else if as you are trying to change display of same element for 2 different conditions. else if您尝试更改2个不同条件的相同元素的显示, else if需要使用else if When you are clicking on third it was fine as per your code, because it was not going to the else of the third condition. 当你点击第三个时,你的代码很好,因为它没有进入第三个条件的其他条件。

When you were clicking on the second one, it was going to the second if and 3rd else as well, so box was not coming. 当你点击第二个时,它会转到第二个if和第三个,所以盒子没有来。

 function radioCheck() { if (document.getElementById('directradio').checked) { document.getElementById('showbank').style.display = 'block'; } else { document.getElementById('showbank').style.display = 'none'; } if (document.getElementById('chequeradio').checked) { document.getElementById('showcashbox').style.display = 'block'; } else if (document.getElementById('cashradio').checked) { document.getElementById('showcashbox').style.display = 'block'; } else { document.getElementById('showcashbox').style.display = 'none'; } } 
 <div class="form-group"> <label class="radio-inline"> <input type="radio" onclick="javascript:radioCheck();" name="optionsRadiosInline" id="directradio" value="option1">Direct </label> <label class="radio-inline"> <input type="radio" onclick="javascript:radioCheck();" name="optionsRadiosInline" id="chequeradio" value="option2">Cheque </label> <label class="radio-inline"> <input type="radio" onclick="javascript:radioCheck();" name="optionsRadiosInline" id="cashradio" value="option3">Cash </label> </div> <div class="col-md-12" id="showbank" style="display:none;"> <div class="form-group"> <label class="col-sm-4 control-label">Comment </label> <div class="col-sm-6"> <textarea type="text" rows="3" class="form-control" name="comment" id="comment"></textarea> </div> </div> </div> <div class="col-md-12" id="showcashbox" style="display:none;"> <div class="form-group"> <label class="col-sm-4 control-label">Comment </label> <div class="col-sm-6"> <textarea type="text" rows="3" class="form-control" name="comment" id="comment"></textarea> </div> </div> </div> 

Hope this will help you. 希望这会帮助你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM