I have to create an array from the dynamically inputs in my form, and check if there identical values.
我必须从我的表单中的动态输入创建一个数组,并检查是否有相同的值。
Example of the inputs:
输入示例:
<input type="text" data-index="0" id="inputMail0" class="signup-input text-value" name="email[0]" placeholder="e.g. example@url.com">
<input type="text" data-index="1" id="inputMail1" class="signup-input text-value" name="email[1]" placeholder="e.g. example@url.com">
I use the following js code:
我使用以下js代码:
function validation(value,element){
$('form *').removeClass('ignore');
var map = [];
$('.email input[type="text"]').each(function() {
if($(this).val()!="") {
map.push($(this).val())
}
});
$('.email input[type="text"]').each(function(){
if (map.indexOf($(this).val())>=0){
console.log($(this).val())
}
});
The code doesn't get the necessary result.
代码没有得到必要的结果。
1 个解决方案
#1
2
Try to merge with one each loop
尝试合并每个循环
var map = [];
$('.email input[type="text"]').each(function(){
if (map.indexOf($(this).val()) == -1){ // check the value exists
map.push($(this).val())
}
else{
// already exists
}
});
#1
2
Try to merge with one each loop
尝试合并每个循环
var map = [];
$('.email input[type="text"]').each(function(){
if (map.indexOf($(this).val()) == -1){ // check the value exists
map.push($(this).val())
}
else{
// already exists
}
});