JavaScript console.dir和运行时

时间:2016-07-24 10:21:21

标签: javascript jquery html object console.dir

我正在尝试使用javascript的对象来保存我的主要元素等。我的对象将从HTML元素中获取内容。我正在尝试使用console.dir来查看我的数据是否被正确抓取。

问题是,console.dir一次显示所有值,而是逐个添加每个元素。我正在使用jQuery的每个函数。

这是小提琴;

https://jsfiddle.net/365dzdhh/9/

这是代码;

(function(jQuery) {
    'use strict';

  window.DOER = {

    saver:function(e){
        e.preventDefault();

      var sel = jQuery('.sel');

      var data = {};

      sel.find('.sell-me').each(function(){
        //Shouldn't this show values one by one instead of all in one at once?
        console.dir(data);
        data = window.DOER.grabber(jQuery(this), data);
      });
      console.dir(data);
    },

    grabber:function(el,data){

        data[jQuery(el).attr('id')] = jQuery(el).val();

      return data;
    },

    init:function(){
        jQuery(document).on('click', '.save-it', this.saver);
    }
  };

}($));

$(document).ready(function(){
    window.DOER.init();
});

如果您检查控制台,您会看到“15”行中的日志每次都显示每个细节。它不应该通过逐个循环来添加每个值吗?我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为这将为您提供所需的控制台信息。第15行上的console.dir(this)。

(function(jQuery) {
    'use strict';

  window.DOER = {

    saver:function(e){
        e.preventDefault();

      var sel = jQuery('.sel');

      var data = {};

      sel.find('.sell-me').each(function(dataForEach){
        //Shouldn't this show values one by one instead of all in one at once?
        console.dir(this);
        data = window.DOER.grabber(jQuery(this), data);
      });
      console.dir(data);
    },

    grabber:function(el,data){

        data[jQuery(el).attr('id')] = jQuery(el).val();

      return data;
    },

    init:function(){
        jQuery(document).on('click', '.save-it', this.saver);
    }
  };

}($));

$(document).ready(function(){
    window.DOER.init();
});