2017-03-08 106 views
0

我一直在破坏我的大脑,试图弄清楚为什么我在下面的代码中第一次按下时,为数组statusIdValues得到一个未定义的错误。它在一个准备就绪的功能中被初始化,并在复选框状态改变时被推入。jQuery数组未定义错误

$(document).ready(function() { 
    var statusIdValues = []; 

     $(':checkbox').change(function() { 

     if ($(this).is(":checked")) { 
      statusIdValues.push($(this).attr("value")); 
     } 
     else { 
      var index= statusIdValues.indexOf($(this).attr("value")); 
      if (index > -1) { 
      statusIdValues.splice(index, 1); 
     } 
    }); 
}); 

任何帮助,将不胜感激。

+2

'array.indexOf($(本).attr(“value”));'什么是数组? –

+0

错字,虐待更新 –

回答

1

在别的试试这个,改变变量 “statusIdValues” 为 “指数” 和 “阵” 为您的实际阵列 “statusIdValues”,像这样:

$(document).ready(function() { 
    var statusIdValues = []; 

     $(':checkbox').change(function() { 

     if ($(this).is(":checked")) { 
      statusIdValues.push($(this).attr("value")); 
     } 
     else { 
      var index = statusIdValues.indexOf($(this).attr("value")); 
      if (index > -1) { 
      statusIdValues.splice(index, 1); 
     } 
    }); 
}); 
+0

就是这样,你相信我也有第二套眼睛在这个我们都没有发现错字!谢谢。 –

+0

问题已被更新后发布 –

+0

那么,这是答案?那你为什么更新你的问题来配合这个?现在有甚么问题吗? –