当选择“任何”时,取消选中组中的所有复选框。复选框被选中用于工作不再工作

发布于 2024-10-17 12:43:50 字数 2866 浏览 4 评论 0原文

我的这个 JS 已经工作了一段时间了,我不知道为什么它现在不再工作了。当选中任意数量的其他复选框时,应该取消选中“Any”(默认选中),并且如果选中“Any”,则取消选中选中的任何内容。

/**
 * Define a set of checkbox groups
 */
var locations = new Array('A', 'P', 'PR', 'GR', 'MC', 'F', 'S', 'L', 'LU', 'E');
/**
 * Checking the "anyId" checkbox clears the rest of the group.  Checking any other clears the "anyId" checkbox.
 */
function toggleBoxes( el, anyId, group ) {
    var i;
    if ( el.id == anyId ) {
        for( i = 0; i < group.length; i++ ){
            var cb = document.getElementById(group[i]);
            cb.checked = false;
        }
    } else {
        var cb = document.getElementById(anyId);
        cb.checked = false; 
    }
}

这是 HTML

<div class="input">
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='anyLocation' name="ts-in_pr" value="any" type="checkbox" checked>Any</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='A' class="geoloc" name="ts-in_pr[]" value="AR" type="checkbox">Ao</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='P' class="geoloc" name="ts-in_pr[]" value="PI" type="checkbox">Pa</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='PR' class="geoloc" name="ts-in_pr[]" value="PO" type="checkbox">Po</p>      
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='GR' class="geoloc" name="ts-in_pr[]" value="GR" type="checkbox">Gr</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='MC' class="geoloc" name="ts-in_pr[]" value="MC" type="checkbox">MC</p>                                                                  
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='F' class="geoloc" name="ts-in_pr[]" value="FI" type="checkbox">Fi</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='S' class="geoloc" name="ts-in_pr[]" value="SI" type="checkbox">Si</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='L' class="geoloc" name="ts-in_pr[]" value="LI" type="checkbox">Li</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='LU' class="geoloc" name="ts-in_pr[]" value="LU" type="checkbox">Lu</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='E' class="geoloc" name="ts-in_pr[]" value="El" type="checkbox">E</p> 
        </div>

发生的情况是,当选中任何其他框时,Any 不会被选中,但是当它被选中时,它只会取消选中下面的三个框(其值为 Ao、Pa 和 Po)。所有其他的都保持选中状态,当手动取消选中它们时,它们也会取消选中“任何”(如果已选中)。我定制了这个 JS,但我不知道它出了什么问题。预先感谢您的任何帮助。

I have this JS that has been working fine for a while, and I have no idea why it is not working anymore now. It is supposed to uncheck Any (checked by default) when any number of other checkboxes is checked, and uncheck whatever is checked if Any is checked.

/**
 * Define a set of checkbox groups
 */
var locations = new Array('A', 'P', 'PR', 'GR', 'MC', 'F', 'S', 'L', 'LU', 'E');
/**
 * Checking the "anyId" checkbox clears the rest of the group.  Checking any other clears the "anyId" checkbox.
 */
function toggleBoxes( el, anyId, group ) {
    var i;
    if ( el.id == anyId ) {
        for( i = 0; i < group.length; i++ ){
            var cb = document.getElementById(group[i]);
            cb.checked = false;
        }
    } else {
        var cb = document.getElementById(anyId);
        cb.checked = false; 
    }
}

And this is the HTML

<div class="input">
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='anyLocation' name="ts-in_pr" value="any" type="checkbox" checked>Any</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='A' class="geoloc" name="ts-in_pr[]" value="AR" type="checkbox">Ao</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='P' class="geoloc" name="ts-in_pr[]" value="PI" type="checkbox">Pa</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='PR' class="geoloc" name="ts-in_pr[]" value="PO" type="checkbox">Po</p>      
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='GR' class="geoloc" name="ts-in_pr[]" value="GR" type="checkbox">Gr</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='MC' class="geoloc" name="ts-in_pr[]" value="MC" type="checkbox">MC</p>                                                                  
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='F' class="geoloc" name="ts-in_pr[]" value="FI" type="checkbox">Fi</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='S' class="geoloc" name="ts-in_pr[]" value="SI" type="checkbox">Si</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='L' class="geoloc" name="ts-in_pr[]" value="LI" type="checkbox">Li</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='LU' class="geoloc" name="ts-in_pr[]" value="LU" type="checkbox">Lu</p>
            <p><input onClick='toggleBoxes(this, "anyLocation", locations)' id='E' class="geoloc" name="ts-in_pr[]" value="El" type="checkbox">E</p> 
        </div>

What happens is that Any is unchecked fine when any other box is checked, however when it is checked back it only unchecks the three boxes right below (which values are Ao, Pa, and Po). All others are kept checked, and when they are unchecked manually, they also uncheck Any (if it was checked). I had this JS custom made, and I have no clue what is going wrong with it. Thanks in advance for any help.

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

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

发布评论

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

评论(4

万劫不复 2024-10-24 12:43:50

我想你在文档中的某个地方有重复的ID(可能是“GR”)。尝试使用前缀以避免此问题。

I suppose you have duplicate id somewhere in document (possibly "GR"). Try using prefix in order to avoid this problem.

凉薄对峙 2024-10-24 12:43:50

这是一个工作示例。我稍微简化了你的函数参数。我看到的代码不需要第二个和第三个参数,因为它们是常量。

http://jsfiddle.net/srEuM/

鲍勃

Here is a working example. I have simplified your function parameters a bit. The code that I see doesn't need the second and third parameter since they are constant.

http://jsfiddle.net/srEuM/

Bob

别在捏我脸啦 2024-10-24 12:43:50

你的代码没问题。我在本地测试了一下,没有问题。

Your code is ok. I tested it locally and no problem.

北方。的韩爷 2024-10-24 12:43:50

您的 javascript 代码必须位于 head 或 body 标记的顶部,但无论哪种方式,您的变量 locations 都需要是全局的,因为每次调用 toggleBoxes 时,< code>locations 将是未定义的。

检查此处的示例: http://jsfiddle.net/ArtBIT/DBanG/

Your javascript code must be in the head, or on the top of the body tag, but either way your variable locations needs to be global since every time you call toggleBoxes, locations will be undefined.

Check the example here: http://jsfiddle.net/ArtBIT/DBanG/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文