我有4个输入,2个文本框和2个单选按钮,我需要获取数组中的对象的值并与之比较,看看它们是否完全匹配。四个输入是:
<input type="text" id="brand">
<input type="text" id="prodNum">
电台1
<input type="radio" name="radioYN" id="radioY" value="Yes">
<input type="radio" name="radioYN" id="radioN" value ="No">
电台2
<input type="radio" name="radioStock" id="inStock" value="Ship">
<input type="radio" name="radioStock" id="outStock" value="Order">
然后我接受这些值并将它们放在变量中
var brand=$('input#brand').val();
var prodNum=$('input#prodNum').val();
var radioYN= $("input[name='radioYN']");
var radioStock= $("input[name='radioStock']");
所有这一切都很好,但我不知道如何将它与数组进行比较,看看它们是否完美匹配.Array:
var products = [
{
"brand": "brand1",
"prodNum": "01-005",
"YN": "Yes",
"Stock": "Order"
},
{
"brand": "brand2",
"prodNum": "02-005",
"YN": "Yes",
"Stock": "Ship"
},
{
"brand": "brand1",
"prodNum": "01-008",
"YN": "No",
"Stock": "Order"
}
]
我会做if语句以查看它们是否匹配
if (inputs == array obeject){
//do something}
else {
//do something else}
答案 0 :(得分:2)
循环使用products数组中的项目,并将所有字段与输入值进行比较。类似的东西:
for (var i=0; i<products.length; i++) {
var product = products[i];
if (product.brand === brand && product.prodNum === prodNum && product.YN === radioYN && product.Stock === radioStock) {
// do something
}
else {
// do something else
}
}