【问题标题】:indexOf- getting '0' though the item is present more than onceindexOf- 尽管该项目不止一次出现,但得到'0'
【发布时间】:2015-06-23 01:21:59
【问题描述】:

我必须从表单中的动态输入创建一个数组,并检查是否有相同的值。

输入示例:

<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">

我使用下面的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())
            }
        });

代码没有得到必要的结果。

【问题讨论】:

  • 你的班级邮箱在哪里?
  • 您似乎很困惑 - 0 的索引意味着在数组的第一个位置找到了该项目。 -1 表示没有找到。
  • @tabz100- 在作为输入父级的 div 中

标签: jquery arrays validation input compare


【解决方案1】:

尝试在每个循环中合并一个

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
            }
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多