简体   繁体   中英

Jquery inArray not returning correct value

In Firebug when I rollover the SelectList variable it certainly looks like an array.

if (GroupList != "") {
    $('select[name^=DDLColumns1] option').each(function(){
        if ($(this).val() != "-1") {
            var ItemsArray = $(this).val().split('|');
            var DataTypes = ItemsArray[1];
            var TestItem = "[" + ItemsArray[0] + "]";

PROBLEM IS HERE---> if (jQuery.inArray(TestItem, SelectList) != -1) {
                    if(DataTypes == 104)
                        NewSelectList += " SUM(CAST(" + ItemsArray[0] + " AS INT)) as " + ItemsArray[0] + ",";
                else
                    NewSelectList += " max(" + ItemsArray[0] + ") as " + ItemsArray[0] + ",";
            }

        }
    });
if(NewSelectList.length > 0) {
        NewSelectList = NewSelectList.substring(0, NewSelectList.length - 1);
        SelectList = NewSelectList;
    }

}//end of if GroupList is not empty

What about cleaning up that mess first? Your errors should get clear if you do it.

if (GroupList != "") {
    $('select[name^=DDLColumns1] option').each(function() {
        if ($(this).val() != "-1") {
            var ItemsArray = $(this).val().split('|');
            var DataTypes = ItemsArray[1];
            var TestItem = "[" + ItemsArray[0] + "]";

            if (jQuery.inArray(TestItem, SelectList) != -1) {
                if(DataTypes == 104)
                    NewSelectList += " SUM(CAST(" + ItemsArray[0] + " AS INT)) as " + ItemsArray[0] + ",";

                else // <--- why all of a sudden no {}?

                    NewSelectList += " max(" + ItemsArray[0] + ") as " + ItemsArray[0] + ",";
            }
            //} //<--- why is commented out? it breaks everything

        } //<-- this closes the callback

}); //<-- broken close of the if

if(NewSelectList.length > 0) {
    NewSelectList = NewSelectList.substring(0, NewSelectList.length - 1);
    SelectList = NewSelectList;
}



} // <---- what is this for? yeah it's broken

PS: Normally variables start with a lowercase letter and classes start with an uppercase one

   if($.isArray( SelectList ) == false)
    SelectList = SelectList.split(',');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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