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

时间:2018-06-21 14:24:43

标签: javascript jquery html css

<!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的功能

1 个答案:

答案 0 :(得分:0)

如果您已经完成了条件

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>

我不确定为什么在表单中有表单,但是没关系....上面的以下代码应该可以帮助您进行测试。如果我误解了您的问题,请随时进行澄清。