简体   繁体   中英

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)I have totally three text boxes i am having a rest api which gets all the data i want to search the matching records

2) if the user enters the start date we should get all the matching records of start date matching to the text

3)i am having 3 text fields i want to get data which is only matching to all the three text Fields (means common to start date , id and title)

4)I am done with or condition but i coudn't figure out how to match the requirement of 3 step please help me i want both 2 and 3 functionalities

If you already have the conditional done

if (a ==obj[i]["ProductID"]  || b==obj[i]["Title"] || c==obj[i]["startDate"] ){
    console.log(a);
    console.log(b);
  }

this contitional. Then requirement 3, for all three matching would be

if (a ==obj[i]["ProductID"]  && b==obj[i]["Title"] && c==obj[i]["startDate"] ){
    console.log(a);
    console.log(b);
}

this would only return true if all three text feilds are matching.

I would change your for loop to something more like

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);
}

as for requirement 2, something like

 if (c==obj[i]["startDate"] ){
        console.log(a);
        console.log(b);
        console.log(C);
  }

Would get you all the records for each element matching the start date making your for loop look like

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);
   }
}

As for the bottom of your page. I would change

}) // Missing semicolon 
}
    </script>
   </body>
 </form> <!-- Needs to be closed inside of the body tag -->
</html>

to this:

}); 
}
    </script>
    </form>
   </body> 
</html>

I'm not sure why you have a form inside of a form, but okay.... The following code above should help you with your test. If I have misunderstood your question please feel free to clarify.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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