简体   繁体   English

jQuery嵌套函数循环两次

[英]jQuery nested functions looping twice

In my limited understanding of jQuery, I don't understand what is happening here. 在我对jQuery的有限理解中,我不明白这里发生了什么。 I have two divs, each with a class of "required". 我有两个div,每个div都有一个“ required”类。 The 1st div contains a set of checkboxes, while the second contains radio buttons. 第一个div包含一组复选框,第二个div包含单选按钮。 The object is to highlight the appropriate div background if no objects in either set are selected. 如果在任何一组中均未选择任何对象,则该对象将突出显示适当的div背景。

The 2nd set, however (the radio buttons), does have a button pre-selected by default, so should never be highlighted. 但是,第二组(单选按钮)确实默认情况下已预先选择了一个按钮,因此绝对不要突出显示。

Here is what is happening : Both sets are being looped through twice. 这是发生的情况 :两组都循环两次。 At the end of the 1st div (checkbox) loop, the div is correctly highlighted. 在第一个div(复选框)循环的末尾,div正确突出显示。 The radio button items are them checked and the default checked button is recognized and that div is not highlighted. 单选按钮项被选中,默认选中的按钮被识别,并且div未突出显示。 Then, the first div items are again gone through. 然后,第一个div项再次经过。 At the end of this - and BEFORE the 2nd div is looped through for the second time, the 2nd div is highlighted. 在此之后-在第二个div循环通过之前,第二个div被突出显示。 I am assuming that the cause here, even though it is correctly looping through the checkboxes, it is associating them with the id of the radiobuttons. 我假设这里的原因,即使它正确地遍历了复选框,也正在将它们与单选按钮的ID相关联。 What I need to do is prevent this second loop-through. 我需要做的是防止第二次循环通过。

$(document).ready(function () {
    $('#Submit').click(function () {
        $('div.Required', '#JobForm').each(function () {
            var FieldName = this.id;
            var FieldLength = FieldName.length
            var CheckError = true;
            $("input:checkbox").each(function () {
                if (this.checked) {
                    CheckError = false;
                }
            });
            if (CheckError) {
                ErrorList += FieldName + ' requires a selection\n';
                this.style.backgroundColor = "#FFC0C0";
            }
            CheckError = true;
            $("input:radio").each(function () {
                if (this.checked) {
                    CheckError = false;
                }
            });
            if (CheckError) {
                ErrorList += FieldName + ' requires a selection\n';
                this.style.backgroundColor = "#FFC0C0";
            }
        });
        if (Error) {
            $('#Submit').val('Submit');
            alert(ErrorList)
            return false;
        } else {
            document.JobForm.submit();
        }
    });
});

Thanks to adeneo's suggestions , I was able to limit the double div looping, which also allowed me to eliminate the additional error message. 多亏了adeneo的建议 ,我才能够限制double div循环,这也使我消除了其他错误消息。 As adeneo stated, since there are two div's, as originally written, the code was looping through both input types each time. 正如adeneo所说,由于最初有两个div,因此代码每次都在两种输入类型之间循环。 As written below, the first loop loops through the checkboxes, while the second loops through the radio buttons. 如下所述,第一个循环遍历复选框,而第二个循环遍历单选按钮。

$(document).ready(function()
{
    $('#Submit').click(function ()
    {
        $('div.Required','#JobForm').each(function()
        {
            var FieldName = this.id;
            var FieldLength = FieldName.length
            var CheckError = true;
            $("input:checkbox", this).each(function ()
            {
                if(this.checked){CheckError=false;}
            });
            $("input:radio", this).each(function ()
            {
                if(this.checked){CheckError=false;}
            });
            if(CheckError){ErrorList += FieldName+' requires a selection\n'; this.style.backgroundColor="#FFC0C0";}
        });
        if(Error)
        {
            $('#Submit').val('Submit');
            alert(ErrorList)
            return false;
        }
        else
        {
            document.JobForm.submit();
        }
    });
});

You're not iterating over each DIV twice, but you're iterating over all the inputs the same number of times as you have matching DIV's 您没有对每个DIV进行两次迭代,而是对所有输入进行了相同次数的迭代,即具有匹配的DIV。

$('#Submit').click(function () {

    // loop over the DIV's, lets say two times if there are two elements
    $('div.Required', '#JobForm').each(function () { 
        // ..... other code

        // the code below selects all the checkboxes, both times

        $("input:checkbox").each(function () {
            if (this.checked) {
                CheckError = false;
            }
        });

        // the code below selects all the radiobuttons, both times

        $("input:radio").each(function () {
            if (this.checked) {
                CheckError = false;
            }
        });

     // .......more code
    });
});

Limit the selection of inputs with context or find() 使用context或find()限制输入的选择

$("input:checkbox", this).each(function () { ...

or 要么

$(this).find("input:checkbox").each(function () { ...

also, your conditions don't make sense, you keep setting CheckError to a boolean, and on the next line you're checking for that boolean, and you'll never get a different result 同样,您的条件没有意义,将CheckError设置为布尔值,然后在下一行检查该布尔值,就永远不会得到不同的结果

CheckError = true;
if (CheckError) { ...

is like doing 就像在做

if (true) { ...

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

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