繁体   English   中英

我想在 javascript 中的控制台中显示匹配的记录

[英]i want to display the matching records in the console in javascript

<!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)我完成了或条件,但我不知道如何匹配 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>

我不知道为什么你在一个表单里面有一个表单,但是好吧......上面的代码应该可以帮助你进行测试。 如果我误解了您的问题,请随时澄清。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM