简体   繁体   English

许多复选框切换显示/隐藏

[英]Many Check boxes toggle show/hide

If I have multiple check boxes (like below html code) that, if checked, show the same data on two fields. 如果我有多个复选框(如下面的html代码),如果选中,则在两个字段中显示相同的数据。 So a simple example would be to check "additional monitor" and "visio" and have "* Required" stay shown. 所以一个简单的例子就是检查“附加监视器”和“visio”并显示“* Required”。

Html Code: Html代码:

<table>
<tr>
    <td class="newemp_dataGLCCother">
        <label for="gl_account">GL Acccount:</label>
        <input name="gl_account" id="gl_account" type="text" maxlength="15" />
        <label id="requiredgla" style="display:none"><font size="2" color="red">* Required</font>
        </label>
    </td>
</tr>
<tr>
    <td class="newemp_dataGLCCother">
        <label for="cost_center">Cost Center:</label>
        <input id="" name="cost_center" id="cost_center" type="text" maxlength="15" />
        <label id="requiredcc" style="display:none"><font color="red">*<font size="2"> Required</font></font>
        </label>
    </td>
</tr>
<tr>
    <td>
        <input type="checkbox" name="non_standard_software" value="Additional Monitor" id="additional_monitor" onclick="showReq('requiredgla'); showReq('requiredcc')" />Additional Monitor</td>
</tr>
<tr>
    <td>
        <input type="checkbox" name="non_standard_software" value="AutoCAD" id="autocad" onclick="showReq('requiredgla'); showReq('requiredcc')" />AutoCAD</td>
</tr>
<tr>
    <td>
        <input type="checkbox" name="non_standard_software" value="MapPoint" id="mappoint" onclick="showReq('requiredgla');  showReq('requiredcc')" />MapPoint</td>
</tr>
<tr>
    <td>
        <input type="checkbox" name="non_standard_software" value="Visio" id="visio" onclick="showReq('requiredgla');  showReq('requiredcc')" />Visio</td>
</tr>
<tr>
    <td>
        <input type="checkbox" name="non_standard_software" value="Project" id="project" onclick="showReq('requiredgla');  showReq('requiredcc')" />Project</td>
</tr>
<tr>
    <td class="newemp_dataGLCCother">
        <input type="checkbox" name="non_standard_software" value="other" id="other" onclick="showReq('otherbox'); showReq('requiredgla'); showReq('requiredcc')" />Other:
        <input name="other" id="otherbox" type="text" style="display:none;" />
    </td>
</tr>

Javascript: 使用Javascript:

function showReq(id) {
var e = document.getElementById(id);
if (e.style.display == 'block') 
    e.style.display = 'none';
else 
    e.style.display = 'block';
}

So with this code as is, when you check an odd number of items "* Required" is shown but if you select an even number of items it is not shown. 因此,使用此代码时,当您检查奇数个项目时,会显示“*必需”,但如果您选择偶数项目,则不会显示。

So I was thinking that if you set a variable to "true" and put that in the js function that it would eliminate the toggle if you select multiple items. 所以我想如果你把一个变量设置为“true”并把它放在js函数中,那么如果选择多个项目就会消除切换。

ie: 即:

function showReq(id) {
var dbl = true;
var e = document.getElementById(id);
if (e.style.display == 'block' && dbl === true) 
    e.style.display = 'none';
dbl = false;
else 
    dbl = true;
    e.style.display = 'block';
}

I know that this doesn't work but I'm looking for something similar to this. 我知道这不起作用,但我正在寻找类似的东西。 Please don't use jQuery unless its completely impossible to use JavaScript. 除非完全不可能使用JavaScript,否则请不要使用jQuery。

I've created a fiddle demoing what I think you're trying to accomplish. 我创造了一个小提琴,演示了我认为你想要完成的事情。

As I understand it, you want to loop through all of the checkboxes to determine if any of them are checked. 据我了解,您希望遍历所有复选框以确定是否检查了其中任何一个。 If any of them are checked, both text boxes are required. 如果选中其中任何一个,则需要两个文本框。

It'd be more efficient to set the display value for both "required" messages simultaneously instead of running showReq for each. 为两个“必需”消息同时设置显示值而不是为每个消息运行showReq会更有效。

Here's the method I added, called within showReq to determine if any checkboxes are checked. 这是我添加的方法,在showReq以确定是否选中了任何复选框。

function checkboxesChecked() {
    //get all the checkboxes
    var checkboxes = document.getElementsByName('non_standard_software');
    //loop through checkboxes, if any are checked, return true
    for (var i = 0; i < checkboxes.length; i++)
        if (checkboxes[i].checked) return true;
    return false;
}

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

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