Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript数组返回[“item1”和“item2”,重复数据消除:函数,包含:函数]_Javascript_Jquery_Checkbox - Fatal编程技术网

Javascript数组返回[“item1”和“item2”,重复数据消除:函数,包含:函数]

Javascript数组返回[“item1”和“item2”,重复数据消除:函数,包含:函数],javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我编写了一个函数来检查页面上的所有复选框输入,并将“选中”的输入放入一个数组中。选中的值正确输入,但我也得到了这些“dedup:function,contains:function”值。为什么? 以下是我的功能: var custTreatments = []; function Checkform() { $(':checkbox:checked').each(function(){ custTreatments.push($(this).val()); return c

我编写了一个函数来检查页面上的所有复选框输入,并将“选中”的输入放入一个数组中。选中的值正确输入,但我也得到了这些“dedup:function,contains:function”值。为什么?

以下是我的功能:

var custTreatments = [];

function Checkform() {
  $(':checkbox:checked').each(function(){
    custTreatments.push($(this).val());
    return custTreatments;
  });
}   

Checkform();    
HTML非常简单:

<input type="checkbox" value="item1" />item1
<input type="checkbox" value="item2" />item2
<input type="checkbox" value="item3" />item3
item1
项目2
项目3

谢谢

编辑js实现以:

var custTreatments = [];

function Checkform() {
  $('[type=checkbox]:checked').each(function(){
    custTreatments.push($(this).val());
    return custTreatments;
  });
}   

Checkform();   
更新

我检查一下。我犯了一个错误。图特

$(“:checkbox”)相当于$(“[type=checkbox]”)

您的代码似乎工作正常

测试


你能再复制一遍吗?

你在使用什么库?看起来有什么东西在扩展
对象
或扩展jQuery集合。看起来数组以某种方式被扩展了一些其他方法?看起来有些库正在将这些方法添加到数组中,这应该是无害的。您“获取”这些值是什么意思?你在哪里买的?您是否在您的
custreatments
数组中使用
for in
?除非您向我们提供重现问题的必要信息,否则我们无能为力。请创建演示。因此您建议使用
[type=checkbox]
而不是
:checkbox
?为什么?为什么它能解决OP的问题?请解释你的解决方案。