简体   繁体   English

jQuery数组/字符串搜索

[英]Jquery Array/String Search

I been working a script that will check if the new input 'value/string' is already exist, but so far I cant find the correct/efficient solution. 我正在工作一个脚本,该脚本将检查新输入的“值/字符串”是否已存在,但到目前为止,我找不到正确/有效的解决方案。

<input type="text" class="existingKeywords" value="string1, string2, string3">
<input type="text" class="newKeyword" value=""> 

the value of .newKeyword will defend on user input and once submit button is click, it will check if the newKeyword.val() already exist from .existingKeywords.val(); .newKeyword的值将取决于用户输入,一旦单击提交按钮,它将检查.existingKeywords.val()中是否已经存在newKeyword.val(); separated by ',' 以“,”分隔

I tried but returns 'json parse unexpected character' 我尝试过但返回'json解析意外字符'

  var arr    = JSON.parse("[" + $('.existingKeywords').val() + "]");
  var newKey = $('.newKeyword').val();
  if( jQuery.inArray (newKey, arr) ){
    alert('Found!');
  }

Can someone tell me, why its not working or Is there a better approach than this? 谁能告诉我,为什么它不起作用?或者有比这更好的方法吗?

Thanks! 谢谢!


Thank you so much guys, the script is now perfectly working. 非常感谢你们,脚本现在可以正常运行了。 I want to 'Vote Up' everyone's answer but it requires 15 Reputation, and as of this writing I only have 13 reputation. 我想“投票”每个人的答案,但它需要15个声誉,而在撰写本文时,我只有13个声誉。 And again gracias everyone. 再次感谢大家。

Maybe you could use the good old String.split() ? 也许您可以使用旧的String.split()

Something like this: 像这样:

var arr = $('.existingKeywords').val().split(',');
var newKey = $('.newKeyword').val();

if( jQuery.inArray(newKey, arr) != -1 ){
    alert('Found!');
}
 var arr    = $('.existingKeywords').val().split(',');
  var newKey = $('.newKeyword').val();
  if( $.inArray (newKey, arr) ){
    alert('Found!');
  }

This will check for case-insensitive duplicate values as well and please try getting values of input with ID instead of class it is safer... 这也将检查不区分大小写的重复值,请尝试使用ID而不是类来获取输入的值,这样更安全...

  var array    = $('.existingKeywords').val().split(',');
  var new_array =[] ;

  for(var key in array)
  new_array.push(array[key].toLowerCase());

  var newkeyword = $('.newKeyword').val().toLowerCase();

 if( $.inArray (newkeyword , new_array)!== -1 ){
     //found your value
  }

It is not working because this line: 它不起作用,因为此行:

var arr    = JSON.parse("[" + $('.existingKeywords').val() + "]");

interprets this way: 这样解释:

var arr    = JSON.parse("[string1, string2, string3]");

which is not a correct JSON - strings inside shall be in quotes, otherwise JSON.Parse doesn't know what is string1 . 这不是正确的JSON.Parse内部的字符串应用引号引起来,否则JSON.Parse不知道什么是string1

As other suggested you need to use split here instead, I'd like to add only trimming the leading/trailing spaces: 正如其他建议您需要在此处使用split一样,我只想添加前导/后缀空格:

var arr = $('.existingKeywords').val().split(',').map(function(s) { return s.trim();})
var newKey = $('.newKeyword').val().trim();
if( $.inArray (newKey, arr) != -1 ){
  alert('Found!');
}

But why don't you use select with multiple choises? 但是,为什么不将select与多个select一起使用? It is more convenient that type keywords I think. 我认为键入关键字更方便。

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

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