bdalina bdalina - 1 month ago 6
Ajax Question

How to check if the value is present in the array of inputs

Here is my code

i have an input textbox where I get a new value

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


after clicking submit i already coded it if the text input is empty.
but inside that if not empty i check again if the value is already in the set of inputs

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


this line doesn't work,
$(".lahatkami").each(function(){}
i put a console.log to check inside but not working

Answer

try to this..

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