【问题标题】:Array is giving an undefined in jquery [closed]数组在jquery中给出了一个未定义的[关闭]
【发布时间】:2017-12-06 20:57:25
【问题描述】:

我想获取每个按钮的 id 并将其保存在数组中。但数组看起来未定义。为什么

jQuery 代码:

var arr = new Array();

$(".button").each(function () {
     arr.push($(this).attr('id'));              
})

console.log(arr);//undefined

 }

HTML 代码:

    <div id="1" class="button" data-bind="dxButton: { }"></div>
    <div id="2" class="button" data-bind="dxButton: { }"></div>
    <div id="3" class="button" data-bind="dxButton: { }"></div>
    <div id="4" class="button" data-bind="dxButton: { }"></div>

【问题讨论】:

  • 你真的为每个元素设置了id属性吗?
  • 你的代码没问题。应该管用。 jsbin.com/xepenivohu/3/edit?html,js,output
  • 你是在按钮渲染后运行这个吗?
  • 它对我有用。
  • 可能是时间问题。确保您的 JavaScript 在文档就绪函数中运行,或者在 end 加载的脚本中运行

标签: javascript jquery arrays


【解决方案1】:
 $(function(){
         var arr = new Array();
         $(".button").each(function () {
               arr.push($(this).attr('id'));
        });
        console.log(arr); //Array(4) 0:"1" 1:"2" 2:"3" 3:"4" length:4
      });



<body>
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <div id="1" class="button" data-bind="dxButton: { }"></div>
      <div id="2" class="button" data-bind="dxButton: { }"></div>
      <div id="3" class="button" data-bind="dxButton: { }"></div>
      <div id="4" class="button" data-bind="dxButton: { }"></div>
    </div>
  </div>
</div>
</body>

结果是: 数组(4) 0:“1” 1:“2” 2:“3” 3:“4” 长度:4

【讨论】:

  • 请解释为什么您的代码有效,而 OP 的代码无效
  • 在他的代码中,多了一个括号}
  • 代码必须在Ready函数中,否则可能会执行错误。
  • 我实际上的意思是您的代码在 DOM 加载后执行,但您的答案是正确的,因此您值得一票
【解决方案2】:

在这种情况下使用map() 函数效率更高。

注意:您的代码应该可以正常工作,尝试将其插入到 ready 函数中,以确保 DOM 中的按钮已完全加载:

$(function(){
   //Your logic here
});

希望得到他的帮助。

$(function() {
  var arr = $(".button").map(function() {
    return this.id;
  }).get();

  console.log(arr);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="1" class="button" data-bind="dxButton: { }"></div>
<div id="2" class="button" data-bind="dxButton: { }"></div>
<div id="3" class="button" data-bind="dxButton: { }"></div>
<div id="4" class="button" data-bind="dxButton: { }"></div>

【讨论】:

    【解决方案3】:

    谢谢大家,它就像这样工作。

    var arr = [];
    $(".button").each(function () {
      arr.push($(this).attr('id'));              
            })
    console.log(arr);
    

    【讨论】:

    • [] 和 new Array 之间没有差异(有但至少与您的方案不同)。
    【解决方案4】:

    您示例中的语法有点偏离。你少了一个;,而多了一个}

    清理一下,我们有:

    var arr = new Array();
    $(".button").each(function () {
      arr.push($(this).attr('id'));
    });
    console.log(arr);
    

    请参阅此处的工作示例:https://jsfiddle.net/8Lt2m4u2/

    正如其他人所建议的,这可能是一个时间问题,但是 console.log(arr); 在任何时候都不应该返回 undefined,因为您定义它并将其初始化为一个空数组。即使您的按钮在console.log 时不存在于 DOM 中,它至少应该打印一个空数组。

    需要注意的一个怪癖是console.log 不一定代表您期望的值。详情请参阅this SO question/answer。一种解决方法是使用console.log(JSON.stringify(arr));,如果对象在您在开发者控制台中查看之前发生更改,这将无关紧要。它将打印执行 console.log 时的值。

    【讨论】:

    • 分号;在JS代码中不是必需的。
    猜你喜欢
    • 2015-04-28
    • 2018-12-21
    • 1970-01-01
    • 2014-08-15
    • 2016-03-29
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多