简体   繁体   中英

How can i implement search like ctrl+f with jquery

Hi i am trying to search on page table tbody , the search is should be like ctrl + F search this my html and php code:

Please refer the image and link linked below for the sources.

    <table>
    <thead>
    <tr><td><input type="text" placeholder="Search Student id="searchStudent"> </td>
    <td></td><td></td><td></td><td></td><td></td></tr>
    </thead>

    <tbody>
    <tr>
    <td>
     <span><img src="<?php echo $gdScoreData['profile_pic']; ?>"/></span>
        <p><?php echo $gdScoreData['student_fname'];?> <?php echo $gdScoreData['student_lname'];?></p>
<p><b>Hallticket</b> : S<?php echo $gdScoreData['student_pid']; ?>J<?php echo $jobID; ?></p>
         </td>
    <td></td><td></td><td></td><td></td><td></td></tr>

1st tr will close here php code is the above one in html it looks like shown in image:

<tr><td><span><img src="profile_pic/first_1479536519.jpg" width="50" /></span><p>Robert Smith</p><p><b>Hallticket</b> : S27J2</p></td></tr>
</tbody>
</table>

This my javascript code to search like ctrl + F

/* Search Student like ctrl+f start */
$("#searchStudent").on("keyup", function() {
    var value = $(this).val();
    console.log(value);
    $("table tbody tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $row.find("td:first").text();

            if (id.indexOf(value) !== 0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});
/* Search Student like ctrl+f end*/

在此输入图像描述

Here is the source from where i have tried: Source JS

i have modified your js fiddle code now it working jsfiddle.net/rFGWZ/3406/

       $("#search").on("keyup", function() {
        var value = $(this).val();
       if(value!='') {
           $("table tbody tr").hide();
       }else{
           $("table tbody tr").show();
       }
         $('table tbody tr td:contains("'+value+'")').parent('tr').show(); 
       });

Basically your code is working but it is not picking up elements inside your tag try adding the following:

var id2 = $row.find("b:first").text();

and changing this:

if (id2.indexOf(value) !== 0) {

Then search based on the bold content and it should work

You could try it like this and search on each part individually:

$id = $row.find("td:first");
var id2 = $id.find("b:first").text();
var id3 = $id.find("p:first").text();
if (id2.indexOf(value) !== 0 && id3.indexOf(value) !== 0) {
$("#searchStudent").on("keyup", function() {
    var value = $(this).val();
    if(value!='') {
        $("table tbody tr").hide();
        }else{
        $("table tbody tr").show();
    }
    $('table tbody tr td:contains("'+value+'")').parent('tr').show(); 
});

I have resolved with this help.

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