Is there a way where I can hold all potential arguments in an array?
Hey again (on a roll today).
In jQuery/Javascript is there a way of effectively having this:
var myArray = [ 'zero', 'one', 'two', 'three', 开发者_如何学运维'four', 'five' ];
//get input from user
if (inputFromUser == anythingInArray) {
alert("it's possible!);
}
is it this?
http://api.jquery.com/jQuery.inArray/
You can use inArray
:
var result = $.inArray(inputFromUser, myArray);
if (result >= 0)
{
alert('Result found at index ' + result);
}
jQuery: $.inArray()
if($.inArray('one', myArray) > -1) {}
If you need to do it without jQuery, you can use the Array.prototype.indexOf
method like
if(myArray.indexOf('one')) {}
That is restricted to ECMAscript Edition 5. If a browser doesn't support that do it the classic route:
for(var i = 0, len = myArray.length; i < len; i++) {
if(myArray[i] === 'one') {}
}
Ref.: $.inArray()
精彩评论