简体   繁体   English

jQuery从表中选择行以获取值

[英]Jquery select row from table to grab the values

I have a live search feature on my site and i want users to be able to select the results and the values be passed down to an input field. 我在网站上具有实时搜索功能,希望用户能够选择结果,并将值向下传递到输入字段。

If i have a static table, the code below works fine but if i perform a search from an mysql table, its not finding the rows. 如果我有一个静态表,下面的代码可以正常工作,但是如果我从mysql表执行搜索,则找不到行。 I can only select the values from the entire table. 我只能从整个表中选择值。 Any ideas? 有任何想法吗?

From Static Table - THIS WORKS FINE 从静态表-这很不错

    <table class="formatHTML5" >

        <!-- TABLE HEADER-->
        <thead>

            <tr>
                <th>Loan</th><th>Month</th><th>Rate</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="p">Loan</td><td class="i">36</td><td class="n">.02881</td><td class="z">.02881</td>
            </tr>
            <tr>
                <td class="p">Loan</td><td class="i">36</td><td class="n">.02751</td><td class="z">.02881</td>
             </tr>

        </tbody>
     </table>


 <script>
 $("tbody tr").click(function () {
        $('.selected').removeClass('selected');
        $(this).addClass("selected");
        var product = $('.p',this).html();
        var infRate =$('.i',this).html();
        var note =$('.n',this).html();
        var gnote =$('.z',this).html();
        alert(product +','+ infRate+','+ note);
    });
     </script>

FROM LIVE SEARCH - THIS DOES NOT WORK 从实时搜索-无效

      index.php
                <div class="row mt">
                    <div class="col-lg-12">
                        <div class="content-panel tablesearch">

                            <section id="unseen">
                                <table id="resultTable" class="table table-bordered table-hover table-condensed formatHTML5">
                                    <thead>

                                        <tr>

                                            <th class="small">Name</th>
                                            <th class="small">Company</th>
                                            <th class="small">Zip</th>
                                            <th class="small">City</th>

                                        </tr>
                                    </thead>

                                    <tbody>

                                    </tbody>
                                </table>
                            </section>

                        </div><!-- /content-panel -->
                    </div><!-- /col-lg-4 -->
                </div><!-- /row -->


  search.php

  // Output HTML formats

  $html = '<tr>';
  $html .= '<td class="small">nameString</td>';
  $html .= '<td class="small">compString</td>';
  $html .= '<td class="small">zipString</td>';
  $html .= '<td class="small">cityString</td>';
  $html .= '</tr>';

// Check for results
    if (isset($result_array)) {
    foreach ($result_array as $result) {
    // Output strings and highlight the matches
     $d_name = preg_replace("/".$search_string."/i", "<b>".$search_string."</b>", $result['name']);
     $d_comp = $result['company'];
     $d_zip = $result['zip'];
     $d_city = $result['city'];
    // Replace the items into above HTML
    $o = str_replace('nameString', $d_name, $html);
    $o = str_replace('compString', $d_comp, $o);
    $o = str_replace('zipString', $d_zip, $o);
    $o = str_replace('cityString', $d_city, $o);
    // Output it
    echo($o);
        }
    }else{
    // Replace for no results
    $o = str_replace('nameString', '<span class="label label-danger">No Names Found</span>', $html);
    $o = str_replace('compString', '', $o);
    $o = str_replace('zipString', '', $o);
    $o = str_replace('cityString', '', $o);
    // Output
    echo($o);
   }
   }

Can you please try with following code: 您可以尝试以下代码吗?

<script type="text/javascript">
    $(document).ready(function(){
     $(document).on("click", "tbody tr",function () {
            $('.selected').removeClass('selected');
            $(this).addClass("selected");
            var product = $('.p',this).html();
            var infRate =$('.i',this).html();
            var note =$('.n',this).html();
            var gnote =$('.z',this).html();
            alert(product +','+ infRate+','+ note);
        });
    });
    </script>

Directly assigning .click() event handler needs that particular element be present in HTML DOM structure. 直接分配.click()事件处理程序需要HTML DOM结构中存在特定元素。 But if that particular element is not present at the time of event listener assignment then it will not get executed. 但是,如果在事件侦听器分配时不存在该特定元素,则将不会执行该元素。 If you know any particular element will exist in DOM in future then we generally go for event delegation technique. 如果您知道将来在DOM中会存在任何特定元素,那么我们通常会采用event委托技术。 In this technique we attach the event listener to document instead of actual DOM element. 在这种技术中,我们将事件侦听器附加到document而不是实际的DOM元素。 So on clicking on element event first gets listen by document and then delegated to actual DOM element. 因此,在单击元素事件时,首先按document进行监听,然后将其委派给实际的DOM元素。

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

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