最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i implement search like ctrl+f with jquery - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jan 31, 2017 at 10:14 Mr world wide asked Jan 31, 2017 at 10:04 Mr world wideMr world wide 4,8449 gold badges45 silver badges105 bronze badges 2
  • What exactly do you want? Your jsfiddle-Code seems to work well... – Andy Commented Jan 31, 2017 at 10:09
  • That is not my js fiddle i am trying to implement that but it is not working for me. the search is not working. i think error is somewhere here: var id = $row.find("td:first ").text(); – Mr world wide Commented Jan 31, 2017 at 10:10
Add a ment  | 

3 Answers 3

Reset to default 7

i have modified your js fiddle code now it working jsfiddle/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.

发布评论

评论列表(0)

  1. 暂无评论