<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
</head>
<body>
<form id = 19>
<form name = "login">
<h4>product ID </h4>
<input type = "text" id = "100">
<h4> Product Title</h4>
<input type = "text" id = "101">
<h4> startdate</h4>
<input type = date id = "102">
<br><button type="submit" form="nameform" onclick="goodfunction()" value="Submit">Submit</button>
</form>
<script>
function goodfunction(){
var url = "xxxxxxxxxxxxxxxx";
$.getJSON(url, function( data ) {
console.log(data);
//console.log("AllData")
var obj = data['AllData'];
console.log(obj);
var l = obj.length
console.log(l);
for(var i=0;i<obj.length;i++){
var a= $( "#100" ).val();
var b= $( "#101" ).val();
var c = $( "#102" ).val();
if (a ==obj[i]["ProductID"] || b==obj[i]["Title"] || c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
}
}
})
}
</script>
</body>
</form>
</html>
1) 我总共有三个文本框,我有一个获取所有数据的 rest api 我要搜索匹配的记录
2) 如果用户输入开始日期我们应该得到开始日期与文本匹配的所有匹配记录
3) 我有 3 个文本字段,我想获取仅与所有三个文本字段匹配的数据(意味着开始日期、ID 和标题通用)
4)我已经完成了 or 条件但我不知道如何满足 3 步的要求请帮助我我想要 2 和 3 功能
最佳答案
如果你已经完成了条件
if (a ==obj[i]["ProductID"] || b==obj[i]["Title"] || c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
}
这个条件。然后要求 3,对于所有三个匹配将是
if (a ==obj[i]["ProductID"] && b==obj[i]["Title"] && c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
}
只有当所有三个文本字段都匹配时,这才会返回 true。
我会把你的 for 循环改成更像
var a= $( "#100" ).val();
var b= $( "#101" ).val(); // There is no need to recreate these variables every time. once is enough.
var c = $( "#102" ).val();
for(var i=0;i<obj.length;i++){
if (a ==obj[i]["ProductID"] && b==obj[i]["Title"] && c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
console.log(C);
}
else if (a ==obj[i]["ProductID"] || b==obj[i]["Title"] || c==obj[i]["startDate"]){
console.log(a);
console.log(b);
}
至于要求2,类似
if (c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
console.log(C);
}
将为您获取与开始日期匹配的每个元素的所有记录,使您的 for 循环看起来像
var a= $( "#100" ).val();
var b= $( "#101" ).val(); // There is no need to recreate these variables every time. once is enough.
var c = $( "#102" ).val();
for(var i=0;i<obj.length;i++){
if (c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
console.log(C);
}
if (a ==obj[i]["ProductID"] && b==obj[i]["Title"] && c==obj[i]["startDate"] ){
console.log(a);
console.log(b);
console.log(C);
}
else if (a ==obj[i]["ProductID"] || b==obj[i]["Title"] || c==obj[i]["startDate"]){
console.log(a);
console.log(b);
}
}
至于页面底部。我会改变
}) // Missing semicolon
}
</script>
</body>
</form> <!-- Needs to be closed inside of the body tag -->
</html>
为此:
});
}
</script>
</form>
</body>
</html>
我不确定为什么您在表单中有一个表单,但是没关系……上面的以下代码应该可以帮助您进行测试。如果我误解了你的问题,请随时澄清。
关于javascript - 我想用javascript在控制台中显示匹配的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50970924/