简体   繁体   English

如何使用Jquery或javascript检查输入的值是否存在

[英]How to check entered value is exist or not using Jquery or javascript

I have one textbox and one button and on button I have written below code. 我有一个文本框和一个按钮,在按钮上我写了下面的代码。 problem is suppose first I have entered in textbox 10 than its worked but when another time I enter 10 than also it prints value is not in array. 问题是假设首先我在文本框10中输入的值比其工作时大,但是当我再次输入10时,它的打印值不在数组中。 so pls help me whats the issue... 所以请帮我什么问题...

 jQuery(document).ready(function() { jQuery("#mybutton").live('click',function () { var sel_fam_rel=jQuery("#my_textbox").val(); var ids = []; code =sel_fam_rel; if($.inArray(code,ids) >= 0) { alert("Value is in array"); } else { alert("Value is not in array"); ids.push(code); } }); }); 

This line: 这行:

if($.inArray(code,ids) >= 0)

should be changed to: 应该更改为:

if($.inArray(code,ids) != -1)

and put your ids var outside of click. 并将您的ID变量放在点击之外。

Try the snippet below. 试试下面的代码片段。

 var ids = []; jQuery("button").on('click', function() { var sel_fam_rel = jQuery("#my_textbox").val(); code = sel_fam_rel; if ($.inArray(code, ids) != -1) { alert("Value is in array"); } else { alert("Value is not in array"); ids.push(code); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='text' id='my_textbox'> <button>check</button> 

use below code . 使用下面的代码。 take your ids out side of click event . 将您的ID排除在click事件之外。 as per your code each time when you click button ids reset . 根据您的代码,每次单击按钮ID重置时。

var ids = [];  // declare as global variable
jQuery(document).ready(function()
{
  jQuery("#mybutton").live('click',function () 
  {
    var sel_fam_rel=jQuery("#my_textbox").val();
    code =sel_fam_rel;
    if($.inArray(code,ids) >= 0)
    {
     alert("Value is in array");
    }
   else
   {
     alert("Value is not in array");
     ids.push(code);
    }
 });
});

Create your array var ids=[]; 创建数组var ids=[]; global outside button event, as whenever you click button it is creating new empty array. 全局外部按钮事件,就像您单击按钮时一样,它正在创建新的空数组。 It will fix your problem. 它将解决您的问题。

A few changes are needed: 需要进行一些更改:

 var ids = []; // `ids` needs to be in the global scope to work as you want it, // or you could use a different method like localstorage jQuery(document).ready(function() { jQuery("#mybutton").on('click',function () // use `on` not `live` which is deprecated { var sel_fam_rel=jQuery("#my_textbox").val(); code =sel_fam_rel; if($.inArray(code,ids) != -1) // inArray() returns -1 if the value is not in the array, you can use it the way you have it, IMO (purely subjective), using `!=-1` is preferable as it's more clear what the code in intend to do { alert("Value is in array"); } else { alert("Value is not in array"); ids.push(code); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="my_textbox" value="10"/><br> <input type="button" id="mybutton" value="Click me"/> 

I made a fiddle to your problem, Use indexOf 我摆弄了你的问题,使用indexOf

http://jsfiddle.net/go8o34fq/ http://jsfiddle.net/go8o34fq/

jQuery- jQuery-

var array=["A","B","C","D"];

$('button').click(function(){
    var code=$('input').val();
    if(array.indexOf(code)==-1)
    {
        array.push(code);
       console.log("if "+array)
    }
    else
    {
      console.log("else "+array)
    }
});

Just a bit requirement if your need is case-sensitive then use- code.toUpperCase() 如果您的需求区分大小写,则只需一点一点即可,请使用code.toUpperCase()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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