JQuery方法的奇怪情况&元素定位

发布于 2024-11-16 06:21:36 字数 4411 浏览 0 评论 0原文

有人经历过这种情况吗?

我有一个 JQuery 方法,只要它们按预定位置定位(select => checkbox),它就可以很好地处理选择和复选框。但是当我以相反的方式放置它们时(复选框<=选择),该方法对复选框的“效果”会以某种方式“更改位置”(应该禁用的复选框不是,而应该启用的复选框则被禁用) )。此外,它并不是在所有情况下都会发生,而是“成对”发生(当应该启用一个复选框而没有启用时,以下复选框在不应该启用时启用,然后再次恢复正常,直到下一次相同的情况为止)事情发生了)。

我说的是两个带有 float left & 的大 div。右图,每个子 div 包含一系列子 div,每个子 div 内都有一个选择和一个复选框;选择和复选框在左侧大 div 中不会“反转”,而在右侧大 div 中反转。

我应该指出,这仅影响“倒置”复选框。无论职位如何,都会按预期选择工作。

我尝试通过父 div 的 id、选择类等来分离(通过创建正确的 JQuery 选择器)那些冲突的复选框,但无济于事。如果所有元素都像左侧的选择和右侧的复选框那样定位,那么一切都会正常进行。

代码:

<div>

    <div style="width: 49.5%; float: left;">

        <div>
            <select id="select_01"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
            <input id="check_box_01" type="checkbox" />
        </div>

        <div>
            <select id="select_02"> 
                <option value="first" selected="selected">First option</option> 
                <option value="">Whatever as prompt...</option> 
            </select>
            <input id="check_box_02" type="checkbox" />
        </div>

        <div>
            <select id="select_03">
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>                    
            </select>
            <input id="check_box_03" type="checkbox" />
        </div>

        <div>
            <select id="select_04">
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>                    
            </select>
            <input id="check_box_04" type="checkbox" />
        </div>

    </div>

    <div style="width: 49.5%; float: right;">

        <div>
            <input id="check_box_05" type="checkbox" />
            <select id="select_05"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
        </div>

        <div style="border: 1px solid red;">
            <input id="check_box_06" type="checkbox" />
            <select id="select_06"> 
                <option value="first" selected="selected">First option</option>
                <option value="">Whatever as prompt...</option> 
            </select>
        </div>

        <div style="border: 1px solid red;">
            <input id="check_box_07" type="checkbox" />
            <select id="select_07"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
        </div>

        <div>
            <input id="check_box_08" type="checkbox" />
            <select id="select_08"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
        </div>

    </div>

</div>

$(document).ready(function() {
    $("select, :checkbox").each(function() {
        if ($(this).is("select")) {
            mySelectId = $("#" + $(this).attr("id"));
            if (mySelectId.attr("value") === "") {
                mySelectId.css({
                    "background": "#D3D3D3"
                });
            }
        } else if ($(this).is(":checkbox")) {
            myCheckboxId = $("#" + $(this).attr("id"));
            if (mySelectId.attr("value") === "") {
                myCheckboxId.attr("disabled", "disabled");
            }
        }
    });
});

问题:

问题的屏幕截图

您可以如果您愿意,请与代码进行交互: http://jsfiddle.net/CarlosPF/Unbsb/

任何帮助或建议将不胜感激。预先感谢大家。

顺便说一句,该方法的作用是使那些未选择任何内容的选择变暗,并禁用其相应的复选框(反之亦然)

Has anyone ever experienced this situation?

I've got a JQuery method that works well with selects and checkboxes AS LONG as they are positioned as predetermined (select => checkbox). But the moment I position them the other way around (checkbox <= select) the "effects" of the method on the checkboxes "change places" somehow (a checkbox that should be disabled is not and one that should be enabled is instead disabled). Also, it doesn't happen in all cases but rather "by pairs" (when a checkbox should be enabled and is not, the following checkbox is enabled when it shouldn't and then it reverts to normalcy again until the next time the same thing happens).

I am talking about two large divs with float left & right, each containg a series of children divs with a select and a checkbox inside each child div; selects and checkboxes are not "inverted" in the left large div and are inverted in the right large div.

I should remark that this affects the "inverted" checkboxes only. Selects work as expected no matter how they are positioned.

I,ve tried to separate (by creating the proper JQuery selector) those conflicting checkboxes by id of parent div, by select class, etc. to no avail. Everything works well if all elements are positioned like selects to the left and checkboxes to the right.

The code:

<div>

    <div style="width: 49.5%; float: left;">

        <div>
            <select id="select_01"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
            <input id="check_box_01" type="checkbox" />
        </div>

        <div>
            <select id="select_02"> 
                <option value="first" selected="selected">First option</option> 
                <option value="">Whatever as prompt...</option> 
            </select>
            <input id="check_box_02" type="checkbox" />
        </div>

        <div>
            <select id="select_03">
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>                    
            </select>
            <input id="check_box_03" type="checkbox" />
        </div>

        <div>
            <select id="select_04">
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>                    
            </select>
            <input id="check_box_04" type="checkbox" />
        </div>

    </div>

    <div style="width: 49.5%; float: right;">

        <div>
            <input id="check_box_05" type="checkbox" />
            <select id="select_05"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
        </div>

        <div style="border: 1px solid red;">
            <input id="check_box_06" type="checkbox" />
            <select id="select_06"> 
                <option value="first" selected="selected">First option</option>
                <option value="">Whatever as prompt...</option> 
            </select>
        </div>

        <div style="border: 1px solid red;">
            <input id="check_box_07" type="checkbox" />
            <select id="select_07"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
        </div>

        <div>
            <input id="check_box_08" type="checkbox" />
            <select id="select_08"> 
                <option value="">Whatever as prompt...</option> 
                <option value="first">First option</option>
            </select>
        </div>

    </div>

</div>

$(document).ready(function() {
    $("select, :checkbox").each(function() {
        if ($(this).is("select")) {
            mySelectId = $("#" + $(this).attr("id"));
            if (mySelectId.attr("value") === "") {
                mySelectId.css({
                    "background": "#D3D3D3"
                });
            }
        } else if ($(this).is(":checkbox")) {
            myCheckboxId = $("#" + $(this).attr("id"));
            if (mySelectId.attr("value") === "") {
                myCheckboxId.attr("disabled", "disabled");
            }
        }
    });
});

And the problem:

screencapture of the problem

You can interact with the code if you wish: http://jsfiddle.net/CarlosPF/Unbsb/

Any help or suggestions will be greatly appreciated. Thanks in advance to you all.

By the way, what the method does is to darken those selects with nothing selected and disable its corresponding checkboxes (or viceversa)

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

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

发布评论

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

评论(1

别再吹冷风 2024-11-23 06:21:36

如果超出范围,则将 else if 子句中的 mySelectId 包含在内。

有问题的代码(伪形式):

    if (select) {
        mySelectId = "some value"
        }
    } else if (checkbox) {
        if (mySelectId.attr("value") === "") { // <-- mySelectID was never properly set!
            //do whatever
        }
    }

编辑:
我改变了你的 jQuery:

$(document).ready(function() {
    $("select").each(function() {
        if ( $(this).val() === "") {
            $(this).css("background-color", "#d3d3d3");
            $(this).siblings(":checkbox").attr("disabled", "disabled"); 
        }   
    });
});

mySelectId in your else if clause if out of scope.

the offending code (pseudo form):

    if (select) {
        mySelectId = "some value"
        }
    } else if (checkbox) {
        if (mySelectId.attr("value") === "") { // <-- mySelectID was never properly set!
            //do whatever
        }
    }

EDIT:
I've changed your jQuery:

$(document).ready(function() {
    $("select").each(function() {
        if ( $(this).val() === "") {
            $(this).css("background-color", "#d3d3d3");
            $(this).siblings(":checkbox").attr("disabled", "disabled"); 
        }   
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文