Array length is zero in jQuery

I wrote like this. After sending a click, the loop does not call. But I saw that there is a value, but the length of the array shows "0". (See Figure.) Why is it not in the loop? and $ ('# myVisibleRows'). val (idsString); becoming "empty".

$(document).ready(function() {

        $('tr[@class^=RegText]').hide().children('td');

        var list_Visible_Ids = [];
        var idsString, idsArray;
        alert($('#myVisibleRows').val());
        idsString = $('#myVisibleRows').val();
        idsArray = idsString.split(',');
        $.each(idsArray, function() {
            if (this != "") {
                $('#' + this).siblings(('.RegText').toggle(true));
                window['list_Visible_Ids'][this] = 1;
            }
        });
        $('tr.subCategory1')
        .css("cursor", "pointer")
        .attr("title", "Click to expand/collapse")
        .click(function() {
            //this = $(this);
            $(this).siblings('.RegText').toggle();

            list_Visible_Ids[$(this).attr('id')] = $(this).css('display') != 'none' ? 1 : null;
            alert(list_Visible_Ids[$(this).attr('id')])
        });
        $('#form1').submit(function() {

            idsString = '';
            $.each(list_Visible_Ids, function(key, val) {
                alert(val);
                if (val) {

                    idsString += (idsString != '' ? ',' : '') + key;
                }
            });
            $('#myVisibleRows').val(idsString);                
            form.submit();
        });
    });

alt text

+3
source share
2 answers

Finally, it works like this.

 for (var index in list_Visible_Ids) {
                idsString += (idsString != '' ? ',' : '') + index;
            }
+1
source

$. each of them is intended for enumeration of key / value pairs in OBJECTS, but not arrays, Nick Craver hinted.

0
source

Source: https://habr.com/ru/post/1744708/


All Articles