如何检查输入数组中是否存在值

时间:2016-10-26 04:48:27

标签: html ajax

这是我的代码

我有一个输入文本框,我得到一个新值

这条线不起作用,



  $(document).on('click', '#af_cbms_add_depID', function() 
        {  
            var toclone=$("#newdepID").val();
            var torefer=$("#af_cbms_question_item").val();
              if((toclone!=='') && (toclone>0))
                  { 

                      ///here's the problem

                          $(".lahatkami").each(function()
                               {

                                   input = $(this).val();
                            
                                   //i also tried the other
                                   //if(this.value===toclone)
                                   if(input===toclone) 
                                        {
                                            alert("There is a duplicate value " + this.value);
                                            $('#newdepID').val('');
                                            $('#newdepID').focus();
                                            return false;
                                        }                  
            
                                  else
                                       {
                                            $("#af_cbms_depIDset").append('<input type="text" name="'+torefer+'" id="depID'+toclone+'" class="lahatkami" value="'+toclone+'" style="width:40px !important; text-align:center;" readonly />');
                                            $('#newdepID').val('');
                                            $('#newdepID').focus();
                                      }
                               });


                  }
              else
                  {
                   alert('No dependent question item was set!');
                   $('#newdepID').focus();
                  }
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="textbox" id="newdepID" value="3"/> sample we a number 3 value

    <button id="af_cbms_add_depID">Submit</button> //submits the value

    //here are the inputs
    <input type="textbox" class="lahatkami" id="newdepID" value="3"/>
    <input type="textbox" class="lahatkami" id="newdepID" value="5"/>
    <input type="textbox" class="lahatkami" id="newdepID" value="4"/>

  
&#13;
&#13;
&#13;

点击提交后,如果文本输入为空,我已编码。    但如果不是空的话我会再次检查该值是否已经在输入集

我把一个console.log用于检查内部但不能正常工作

2 个答案:

答案 0 :(得分:1)

尝试这个..

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
        $( "#af_cbms_add_depID" ).click(function()
        {
            var toclone= $("#newdepID").val();
            var torefer=$("#af_cbms_question_item").val();
            if((toclone!=='') && (toclone > 0)) {
                  $(".lahatkami").each(function() {
                           input = $(this).val();
                           //alert(input);
                           if(input===toclone)
                            {
                                alert("There is a duplicate value " + this.value);
                                $('#newdepID').val('');
                                $('#newdepID').focus();
                                return false;
                            }else {
                                $("#af_cbms_depIDset").append('<input type="text" name="'+torefer+'" id="depID'+toclone+'" class="lahatkami" value="'+toclone+'" style="width:40px !important; text-align:center;" readonly />');
                                $('#newdepID').val('');
                                $('#newdepID').focus();
                            }
                    });
            } else {
               alert('No dependent question item was set!');
               $('#newdepID').focus();
            }
    });
    </script>

答案 1 :(得分:0)

我不清楚你的具体要求,但我认为下面是你要找的代码。

$(document).on('click', '#af_cbms_add_depID', function(){  
    var toclone=$("#newdepID").val();
    var torefer=$("#af_cbms_question_item").val();
    $("#af_cbms_depIDset").html("");
    if((toclone!=='') && (toclone>0)){ 
        var $tmpdom = $("<span></span>");
        $(".lahatkami").each(function(){
           input = $(this).val();
           if(input===toclone){
                    alert("There is a duplicate value " + this.value);
                    $(this).val('');
                    $(this).focus();
                    return;
           }else {  
               $tmpdom.append('<input type="text" name="'+torefer+'" id="depID'+toclone+'" class="lahatkami" value="'+toclone+'" style="width:40px !important; text-align:center;" readonly />');
           }
        });
        $("#af_cbms_depIDset").append($tmpdom.html());

      }else{
           alert('No dependent question item was set!');
           $('#newdepID').focus();
     }
});