简体   繁体   中英

How to check if values in an array is exists in an array in jquery/javascript

I've searched over the internet but nothings seems to be the same with my problem. What I want is to add question numbers for the questions in text field, and numbers should be unique . I'm trying to check if the array of numbers is exists in an array of number of questions (eg 8 questions) , then if it is in array, you can enter the number as long as it is not entered yet , but my code is not working. How can I do that? Please see my code below for reference.

 $(document).ready(function(){ var try1; var arrayLen = $('#question\\\\[\\\\]').length; var numArray = []; var convertedArray; for(i = 1; i <= arrayLen; i++){ numArray.push(i); } $('#question\\\\[\\\\]').on('input', function(){ if($.inArray($(this).val(), numArray) !== -1){ $('#result').html("available"); } else{ $("#result").html("not available"); return false; } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="question[]" placeholder="multipleChoice"><br><br> <input type="text" id="question[]" placeholder="trueFalse"><br><br> <input type="text" id="question[]" placeholder="shortAnswer"><br><br> <input type="text" id="question[]" placeholder="shortAnswer"><br><br> <input type="text" id="question[]" placeholder="description"><br><br> <input type="text" id="question[]" placeholder="multipleChoice"><br><br> <input type="text" id="question[]" placeholder="multipleChoice"><br><br> <input type="text" id="question[]" placeholder="trueFalse"><br><br> <span id="result"></span> 

Accepted Answer: but needs an UPDATE

Accepted answer by @Shiladitya

Here you go with a solution https://jsfiddle.net/33zeL2fa/5/

 $(document).ready(function(){ var arrayLen = $('input[name="question[]"]').length; var numArray = []; for(i = 1; i <= arrayLen; i++){ numArray.push(i); } $('input[name="question[]"').keypress(function(e){ if(numArray.indexOf(parseInt(e.key)) != -1){ $('#result').html("available"); numArray.splice(numArray.indexOf(parseInt(e.key)), 1); } else{ $("#result").html("not available"); e.preventDefault(); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="question[]" placeholder="multipleChoice"><br><br> <input type="text" name="question[]" placeholder="trueFalse"><br><br> <input type="text" name="question[]" placeholder="shortAnswer"><br><br> <input type="text" name="question[]" placeholder="shortAnswer"><br><br> <input type="text" name="question[]" placeholder="description"><br><br> <input type="text" name="question[]" placeholder="multipleChoice"><br><br> <input type="text" name="question[]" placeholder="multipleChoice"><br><br> <input type="text" name="question[]" placeholder="trueFalse"><br><br> <span id="result"></span> 

I've used jQuery keydown event.

More Update with validation Her you go https://jsfiddle.net/33zeL2fa/6/

 $(document).ready(function(){ var try1; var arrayLen = $('input[name="question[]"]').length; var numArray = []; var convertedArray = []; for(i = 1; i <= arrayLen; i++){ numArray.push(i); } $('input[name="question[]"').keydown(function(e){ if(e.which === 8 || e.keyCode === 8){ numArray.push(parseInt($(this).attr('newval'))); $(this).removeAttr('newval'); } else { if(numArray.indexOf(parseInt(e.key)) != -1){ $('#result').html("available"); $(this).attr('newval', e.key); numArray.splice(numArray.indexOf(parseInt(e.key)), 1); } else{ $("#result").html("not available"); e.preventDefault(); } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="question[]" placeholder="multipleChoice"><br><br> <input type="text" name="question[]" placeholder="trueFalse"><br><br> <input type="text" name="question[]" placeholder="shortAnswer"><br><br> <input type="text" name="question[]" placeholder="shortAnswer"><br><br> <input type="text" name="question[]" placeholder="description"><br><br> <input type="text" name="question[]" placeholder="multipleChoice"><br><br> <input type="text" name="question[]" placeholder="multipleChoice"><br><br> <input type="text" name="question[]" placeholder="trueFalse"><br><br> <span id="result"></span> 

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM