如何使用jQuery在数组中查找重复值? [英] How to find duplicate values in array using jQuery?
本文介绍了如何使用jQuery在数组中查找重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些输入,其名称是以一种形式排列的数组:
I have some inputs which names is array in one form:
<input type="text" name="email[]" id="email" />
<input type="text" name="email[]" id="email" />
<input type="text" name="email[]" id="email" />
等
我提交表单时需要在jQuery中签入,如果此输入中的值重复,可以帮助任何人吗?
when I submit the form I need to check in jQuery if have a duplicated value in this inputs, can help me anyone?
谢谢:)
推荐答案
我会这样做:
var values = $('input[name="email[]"]').map(function() {
return this.value;
}).toArray();
var hasDups = !values.every(function(v,i) {
return values.indexOf(v) == i;
});
$('form').submit(function(e) {
if (hasDups) e.preventDefault();
});
此外,ID应该像人们在评论中所说的那样唯一.
Also, ids should be unique like people are saying in the comments.
这篇关于如何使用jQuery在数组中查找重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文