单击某个按钮时,我正在遍历DOM元素。我已经将类finish-proc附加到按钮上,因此单击该按钮将激活此功能:

<script>
    $(document).on('click', '.finish-proc', function () {
        var communities = [];

        var $this, $thisDay, input, inputDay, text, textDay, obj, objDay;

        $('.panel-default').each(function (i) {
            var maxPeople = '.' + $(this).attr('data-community') + '-max-people';
            var dayInfoRow = '.' + $(this).attr('data-community') + '-day-info';
            obj = {};
            obj["maxPeople"] = $(maxPeople).val();

            var daysArrayInLoop = [];

            $(dayInfoRow).each(function (j) {
                var objDay = {};

                var dayString = '.' + $(this).attr('data-community') + '-day-' + (j + 1);
                var dayStringStart = '.' + $(this).attr('data-community') + '-day-' + (j + 1) + '-start';
                var dayStringEnd = '.' + $(this).attr('data-community') + '-day-' + (j + 1) + '-end';

                objDay["dayString"] = $(dayString).val();
                objDay["dayStringStart"] = $(dayStringStart).val();
                objDay["dayStringEnd"] = $(dayStringEnd).val();

                daysArrayInLoop.push(objDay);
            }

            obj["dayArray"] = daysArrayInLoop;

            communities.push(obj);
        }
   }
</script>


这段代码断线了:

daysArrayInLoop.push(objDay);


与错误:


  daysArrayInLoop.push不是函数


谁能告诉我为什么呢?

编辑-我试图将var daysArrayInLoop = [];更改为var daysArrayInLoop = {};,仍然收到相同的错误

最佳答案

尝试此代码在推入对象后定义数组

var daysArrayInLoop = new Array();
    daysArrayInLoop.push(obj);

09-26 01:22