It is possible to use the Contains method to determine whether a collection contains an element in C # syntax, but similar questions are not contains in JavaScript.
We can use JS's prototype extension to encapsulate a contains method of our own.
JS Code:
Copy Code code as follows:
<script type= "Text/javascript" >
$ (function () {
Array.prototype.contains = function (Element) {//using the array's prototype prototype to point out a method name that I want to encapsulate contains
for (var i = 0; i < this.length; i++) {
if (this[i] = = Element) {//If an element in the array is equal to the element object you want to test, the proof array contains this element and returns True
return true;
}
}
}
Use an example to verify some of the methods we encapsulate
var $subCategoryID = $ ("#hidSubCategory"). Val ();
var $subCategoryIDs = new Array (); Construct an Array object
$subCategoryIDs = $subCategoryID. Split (","); assigning values to arrays
$ ("Input[type=radio]"). each (function () {
if ($subCategoryIDs. Contains (this). attr ("id")) {//Use the Contains method to determine if the array contains $ (this). attr ("id")
$ (this). attr ("Checked", true);
}
})
})
</script>
Verify that an array object using the Contains method does not have to be explicitly declared, that is, the italic portion of the preceding code can be abbreviated as:
Copy Code code as follows:
var $subCategoryID = $ ("#hidSubCategory"). Val (). Split (",");