Home > Web Front-end > JS Tutorial > Whether the specified value exists in the jQuery array

Whether the specified value exists in the jQuery array

php中世界最好的语言
Release: 2018-04-28 11:43:36
Original
1666 people have browsed it

This time I will bring you jQueryWhether there is a specified value in the array and determine whether there is a specified value in the jQuery arrayWhat are the precautions, as follows This is a practical case, let’s take a look at it.

<!DOCTYPE>
<html>
<head>
<title>jquery判断值是否存在于数组中</title>
<meta charset="utf-8">
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
//参数第一个为数组,第二个为需要判断是否存在于数组中的值
function isInArray(arr,val){
  var str = ","+arr.join(",")+",";
  if(str.indexOf(","+val+",") != "-1"){
    //该数存在于数组中
    arr.splice($.inArray(val,arr),1);  //把该数从数组中删除
    console.log(arr);
  }else{
    //该数不存在于数组中
    console.log("不存在");
  }
}
var arr = [1,28,60,80,6];
isInArray(arr,28);//存在,删除元素28
isInArray(arr,18);//不存在,提示“不存在”
</script>
</body>
</html>
Copy after login

Run results:

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading

How to make a non-empty judgment for el expression in js

Share the page and jump back to the homepage

The above is the detailed content of Whether the specified value exists in the jQuery array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template