简体   繁体   English

正确的JavaScript以显示/隐藏输入

[英]Proper JavaScript to show/hide inputs

I just want to know the proper function loop code for show/hide method. 我只想知道show / hide方法的正确函数循环代码。 This is the javascript for show/hide for the first (1) radio button: 这是用于显示/隐藏第一个(1)单选按钮的javascript:

function showHide(){
    var chckbox = document.getElementById("chk");
    var hiddeninputs = document.getElementsByClassName("hidden");

    for(var i=0; i !=hiddeninputs.length; i++){
        if(chckbox.checked){
            hiddeninputs[i].style.display ="block";
        }
        else{
            hiddeninputs[i].style.display ="none";
        }
    }
}

Yet I need the proper loop for having multiple objects (checkboxes) with seperated show/hide method. 但是,我需要使用单独的show / hide方法使用多个对象(复选框)的适当循环。 This is the first checkbox code: 这是第一个复选框代码:

<input type="checkbox" name="area" id="chk" onclick="showHide(this.checked);"/>
    <label for="chk">Billing & Credit Management Systems</label>

<div class="hidden">
<input type="radio" name="area1" /> <label> Web Billin  g </label> <br />
<input type="radio" name="area1" /> <label> CRIBS </label>  <br />
<input type="radio" name="area1" /> <label> PPC </label>  <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label>  <br />
</div>

And I need the loop code in order to prompt the show/hide to the following objects: 我需要循环代码以提示显示/隐藏以下对象:

<input type="checkbox" name="area" id="chk1" onclick="showHide(this.checked);"/>
    <label for="chk1">Customer Care Systems</label>

<div class="hidden">
<input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br />
<input type="radio" name="area1" /> <label> MVNO CRM </label>  <br />
<input type="radio" name="area1" /> <label> Self-Care Site </label>  <br />
<input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label>  <br />
<input type="radio" name="area1" /> <label> IRS </label>  <br />
<input type="radio" name="area1" /> <label> Online Guide </label>  <br />
<input type="radio" name="area1" /> <label> TMOS </label>  <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label>  <br />
</div>

Only the first checkbox prompts while the second one doesn't whenever I checked. 每当我选中时,仅第一个复选框提示,而第二个则不提示。

Not the exact answer you're looking for, but a bit simpler (less looping). 不是您要找的确切答案,而是更简单(更少的循环)。 Let me know if this works for you, I'll try my best to explain what's happening here. 让我知道这是否对您有用,我将尽力解释这里发生的事情。

I implemented this solution with the intention of forcing you to change as little as possible. 我实施此解决方案的目的是迫使您进行尽可能少的更改。

1) Assign a unique attribute to the checkbox and the div it belongs to. 1)为复选框及其所属的div分配唯一属性。 In this case I used "data-menu". 在这种情况下,我使用了“数据菜单”。 In the onclick function, pass "this" instance of the element into the function showHide. 在onclick函数中,将元素的“ this”实例传递到函数showHide中。

<input data-menu="1" type="checkbox" name="area" id="chk" onclick="showHide(this);"/>

<div class="hidden" data-menu="1">

2) Use the css class 'hidden' to hide your menu options. 2)使用CSS类“隐藏”来隐藏菜单选项。

.hidden {

  display: none;

}

3) Re-work your JS function to dynamically add the hidden class when the box is checked. 3)重做您的JS函数,以在选中该框时动态添加隐藏的类。 Since your menus are off by default, checking on naturally turns them on. 由于默认情况下菜单是关闭的,因此选中自然会打开它们。

function showHide(e){

    var menu = document.querySelector('div[data-menu="'+e.getAttribute('data-menu')+'"');
    menu.classList.toggle('hidden');

}

Check out the below working snippet to see it in action. 请查看以下工作片段,以查看实际效果。

 function showHide(e){ var menu = document.querySelector('div[data-menu="'+e.getAttribute('data-menu')+'"'); menu.classList.toggle('hidden'); } 
 .hidden { display: none; } 
 <input data-menu="1" type="checkbox" name="area" id="chk" onclick="showHide(this);"/> <label for="chk">Billing &amp; Credit Management Systems</label> <div class="hidden" data-menu="1"> <input type="radio" name="area1" /> <label> Web Billin g </label> <br /> <input type="radio" name="area1" /> <label> CRIBS </label> <br /> <input type="radio" name="area1" /> <label> PPC </label> <br /> <input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label> <br /> </div> <input data-menu="2" type="checkbox" name="area" id="chk1" onclick="showHide(this);"/> <label for="chk1">Customer Care Systems</label> <div data-menu="2" class="hidden"> <input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br /> <input type="radio" name="area1" /> <label> MVNO CRM </label> <br /> <input type="radio" name="area1" /> <label> Self-Care Site </label> <br /> <input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label> <br /> <input type="radio" name="area1" /> <label> IRS </label> <br /> <input type="radio" name="area1" /> <label> Online Guide </label> <br /> <input type="radio" name="area1" /> <label> TMOS </label> <br /> <input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label> <br /> </div> 

 function showHide(element){ var chckbox = document.getElementById(element); var hiddeninputs = document.getElementsByClassName(element); for(var i=0; i !=hiddeninputs.length; i++){ if(chckbox.checked){ hiddeninputs[i].style.display ="block"; } else{ hiddeninputs[i].style.display ="none"; } } } 
 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <input type="checkbox" name="area" id="chk" onclick="showHide(this.id);"/> <label for="chk">Billing & Credit Management Systems</label> <div class="chk"> <input type="radio" name="area1" /> <label> Web Billin g </label> <br /> <input type="radio" name="area1" /> <label> CRIBS </label> <br /> <input type="radio" name="area1" /> <label> PPC </label> <br /> <input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label> <br /> </div> <br> <input type="checkbox" name="area" id="chk1" onclick="showHide(this.id);"/> <label for="chk1">Customer Care Systems</label> <div class="chk1"> <input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br /> <input type="radio" name="area1" /> <label> MVNO CRM </label> <br /> <input type="radio" name="area1" /> <label> Self-Care Site </label> <br /> <input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label> <br /> <input type="radio" name="area1" /> <label> IRS </label> <br /> <input type="radio" name="area1" /> <label> Online Guide </label> <br /> <input type="radio" name="area1" /> <label> TMOS </label> <br /> <input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label> <br /> </div> </body> </html> try this this will work fine for you. 

You're not using the parameter you're providing in the call of your function. 您没有使用函数调用中提供的参数。

var chckbox = document.getElementById("chk");

will always find the first checkbox only to know if the other fields should be hidden or not and 总是会找到第一个复选框,仅知道其他字段是否应该隐藏,并且

var hiddeninputs = document.getElementsByClassName("hidden");

will allways hide all elements with class hidden. 将始终隐藏所有具有隐藏类的元素。 I think that 我觉得

<input type="checkbox" name="area" id="chk" onclick="showHide(this);"/>

(ie change the parameter to the element rather than the .checked) in combination with (即,将参数更改为元素而不是.checked)与

function showHide(elem){
    var selector = "#" + elem.id + " ~ .hidden";
    document.querySelector(selector).style.display = elem.checked ? 'block' : 'none';
}

will do more of what you want 将做更多您想要的

call onchange function in input type checkbox 在输入类型复选框中调用onchange函数

example

<input type="checkbox" name="area" id="chk" onchange="valueChanged(this.id)"/>Billing & Credit Management Systems
<input type="checkbox" name="area" id="chk1" onchange="valueChanged(this.id)"/> Customer Care Systems

<script type="text/javascript">

 $(document).ready( function(){

     $(".hidden").hide();
     $(".hidden2").hide();

 });

 function valueChanged(id){

     if(id== "chk"){
         if($('#chk').is(":checked"))   
            $(".hidden").show();
         else
            $(".hidden").hide();
     }

     if(id== "chk1"){
        if($('#chk1').is(":checked"))   
            $(".hidden2").show();
        else
            $(".hidden2").hide();
     }

  }
</script>

<div class="hidden">
 <input type="radio" name="area1" /> <label> Web Billin  g </label> <br />
 <input type="radio" name="area1" /> <label> CRIBS </label>  <br />
 <input type="radio" name="area1" /> <label> PPC </label>  <br />
 <input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label>  <br />
</div>

<div class="hidden2">
    <input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br />
    <input type="radio" name="area1" /> <label> MVNO CRM </label>  <br />
    <input type="radio" name="area1" /> <label> Self-Care Site </label>  <br />
    <input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label>  <br />
    <input type="radio" name="area1" /> <label> IRS </label>  <br />
    <input type="radio" name="area1" /> <label> Online Guide </label>  <br />
    <input type="radio" name="area1" /> <label> TMOS </label>  <br />
    <input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label>  <br />
</div>

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

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