javascript jQuery:根据数组中的值检查输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7152867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: check input value against values in an array
提问by flinx777
I'm attempting to check the values entered by a user into an input text field. Once they enter the value and click submit, if the value they enter matches a value in an array, I'm attempting to produce one result, if not in the array, another result.
我正在尝试检查用户在输入文本字段中输入的值。一旦他们输入值并单击提交,如果他们输入的值与数组中的值匹配,我将尝试生成一个结果,如果不在数组中,则生成另一个结果。
Basically have a simple form like so:
基本上有一个简单的形式,如下所示:
<form id="checkinput" action="#">
<input type="text" value="" id="couponcode" name="coupon code name"/>
<input type="button" id="submit" value="Submit"/>
</form>
When click "#submit", if value in the "#couponcode" input field equals the value in an array, add a class of "success" to a div with an id of "success_show" otherwise a simple alert like "your coupon code is wrong" would be displayed.
单击“#submit”时,如果“#couponcode”输入字段中的值等于数组中的值,则将“success”类添加到 id 为“success_show”的 div 中,否则添加一个简单的警报,例如“您的优惠券代码”错误”将显示。
I'll have a hidden div and upon success, add a class of "success" to the div.
我将有一个隐藏的 div,成功后,向 div 添加一类“成功”。
The array will change periodically and will have different quantities at different times. Something simple like so:
该数组将定期更改,并且在不同时间具有不同的数量。像这样简单的事情:
var arr = [ "CA238", "Pete", 8, "John" ];
回答by gislikonrad
You can use jquery...
你可以使用 jquery ...
$.inArray(value, array) // returns -1 if value doesn't exist, otherwise returns index
EDITIf you don't use jQuery then you can use indexOf on the array
编辑如果你不使用 jQuery 那么你可以在数组上使用 indexOf
if(array.indexOf(value) < 0){ // doesn't exist
// do something
}
else { // does exist
// do something else
}
回答by Rafay
function include(arr, obj) {
for(var i=0; i<arr.length; i++) {
if (arr[i] === obj) return true;
}
}
var arr = [ "CA238", "Pete", 8, "John" ];
$("#submit").click(function(e){
e.preventDefault();
var val=$("#couponcode").val();
if(include(arr, val))
alert("success");
});
hel taken from Best way to find if an item is in a JavaScript array?
hel 取自查找项目是否在 JavaScript 数组中的最佳方法?
OR
或者
use inArray
利用 inArray
$("#submit").click(function(e){
e.preventDefault();
var val=$("#couponcode").val();
if($.inArray(arr,val))
alert("success");
});
回答by ipr101
You could use -
你可以使用 -
function include(arr,obj) {
return (arr.indexOf(obj) != -1);
}
Taken from this question - Best way to find if an item is in a JavaScript array?
取自这个问题 -查找项目是否在 JavaScript 数组中的最佳方法?
回答by Baz1nga
well your array needs to be created like this:
那么你的数组需要像这样创建:
var a=[];
a["couponcode"]="CA238";
a["name"]="peter";
or a json as follows:
或 json 如下:
var a={ couponCode:"CA238", name: "peter"}
with that in place now when you want to validate your form run it through all the inout element be using say $.each
and then get the id of each of the input and get the vlaue of the same from the array that you have created or from the json compare the two values and see if it is correct.
现在当你想验证你的表单时,通过使用 say 的所有 inout 元素运行它$.each
,然后获取每个输入的 id 并从你创建的数组或 json 中获取相同的 vlaue比较这两个值,看看是否正确。
code will be like:
代码将类似于:
$("input").each( function()
{
var id=$(this).attr("id");
if(a[id]) // for json a.hasOwnProperty(id)
{
if($(this).val()!==a[id])
{
// do the necessary
}
}
});