【问题标题】:How to make an entire row clickable in live search如何在实时搜索中使整行可点击
【发布时间】:2019-07-01 20:04:18
【问题描述】:

我正在进行实时搜索。使用 ajax 连续编写链接后,它停止响应

这是我写的代码

//让这个页面名是index.php

<div>
      <input type="search" id="searchterm" name="searchterm" placeholder="Search" >   
</div>
<table id="result"></table>

//这是index.page的脚本

<script>
$(document).ready(function(){
    load_data();
    function load_data(query)
    {
        $.ajax({
            url:"search.php",
            method:"post",
            data:{query:query},
            success:function(data)
            {
                $('#result').html(data);
            }
        });
    }



$('#searchterm').keyup(function(){
        var search = $(this).val();
        if(search != '')
        {
            load_data(search);
        }
        else
        {
            load_data();            
        }
    });
});
</script>

//这个页面是search.php

$output='';
if(isset($_POST["query"]))
 {
    $keyword=mysqli_real_escape_string($conn, $_POST["query"]);
    $query="SELECT uid,name,lastname ,profile_pic,about FROM comnet_user_details WHERE uid!='$uid' AND concat_ws(' ',name, lastname) LIKE UPPER('%$keyword%')";

 $result=mysqli_query($conn,$query);



if(mysqli_num_rows($result) > 0)
 {
while($r=mysqli_fetch_array($result))
    {
        $nm=$r["name"]." ".$r["lastname"];
        $profilepic=$r["profile_pic"];
        $about=$r["about"];
        $id=$r["uid"];





  $output .=
     '<table class="msgtab" id="fetchresult">

      <tr  class="trow" onclick="location.href="conversation.php?value='.$id.'"">

     <td class="msg-col-1"><img src="images/profilepic/'.$r["profile_pic"].'alt="Avatar" class="circlemsg"></td>

     <td class="msg-col-2">
        <h5 class="msgheading">'.$nm.'</h5>
        <p class="msgcontent">'.$about.'</p>

        </td>
        </tr>;
        }
         echo $output;
}

我希望实时搜索结果行应该是可点击的,并且单击特定结果或行应该会导致所需页面,但是我的实时搜索结果链接不起作用

【问题讨论】:

  • 调试起来会有点困难(没有全貌)。 modal is not defined 呢。在这种情况下,这是一个 javascript 错误。
  • 看起来你在 document.ready 中定义了你的 jquery 模态函数。不要那样做。将函数移到外面,这应该可以解决未捕获的引用问题。阅读this questionthis one also

标签: javascript php ajax mysqli livesearch


【解决方案1】:
  • &lt;img src="images/profilepic/'.$r["profile_pic"].'alt="Avatar" class="circlemsg"&gt; 中有错字 必须是&lt;img src="images/profilepic/'.$r["profile_pic"].'" alt="Avatar" class="circlemsg"&gt;

  • 另外,profile_pic 也有扩展名(例如:'jpg, png')?

编辑:

你能用这段代码替换这段代码吗(希望它会起作用): - 创建一个table - 在 while 循环中 -> 创建每一行 &lt;tr&gt; - 然后关闭table

if(mysqli_num_rows($result) > 0) {

   $output = '<table class="msgtab" id="fetchresult">'; 

while($r=mysqli_fetch_array($result))
    {
        $nm=$r["name"]." ".$r["lastname"];
        $profilepic=$r["profile_pic"];
        $about=$r["about"];
        $id=$r["uid"];


    $output .=
    '<a href="conversation.php?value='.$id.'">
    <tr class="trow">
    <td class="msg-col-1"><img src="images/profilepic/'.$r["profile_pic"].'" alt="Avatar" class="circlemsg"></td>
    <td class="msg-col-2">
    <h5 class="msgheading">'.$nm.'</h5>
    <p class="msgcontent">'.$about.'</p>
    </td>
    </tr>
    </a>';

    }

$output .= '</table>';

echo $output;

}

【讨论】:

  • 好的。你知道我应该如何在&lt;tr class="trow" onclick="location.href="conversation.php?value='.$id.'""&gt; 中写链接才能开始工作
  • 如果我理解正确,您想让整行“可点击”,对吗?在这种情况下,最好将&lt;tr&gt; 包装在&lt;a&gt; 中。即&lt;a href="conversation.php?value=$id"&gt;&lt;tr&gt;...&lt;/tr&gt;&lt;/a&gt;
  • 是的,您理解正确。正如你所说,我已经尝试过:&lt;a href="conversation.php?value='.$id.'"&gt; &lt;tr class="trow"&gt; &lt;td class="msg-col-1"&gt;&lt;img src="images/profilepic/'.$r["profile_pic"].'" alt="Avatar" class="circlemsg"&gt;&lt;/td&gt; &lt;td class="msg-col-2"&gt; &lt;h5 class="msgheading"&gt;'.$nm.'&lt;/h5&gt; &lt;p class="msgcontent"&gt;'.$about.'&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/a&gt;。但它仍然无法正常工作
  • 感谢您的努力。我复制了代码并粘贴了它。但是,没有运气。还是不负责任
  • 您能否提供这些信息: - 输出结果的屏幕截图(在浏览器上的显示) - 链接在 devtool 中的显示 - 单击行时会发生什么?
【解决方案2】:

好的..经过多次工作后,我找到了解决方案,并且对我有用。是这样的

if(mysqli_num_rows($result) > 0) {

   $output = '<table class="msgtab" id="fetchresult">'; 

while($r=mysqli_fetch_array($result))
    {
        $nm=$r["name"]." ".$r["lastname"];
        $profilepic=$r["profile_pic"];
        $about=$r["about"];
        $id=$r["uid"];


    $output .=
'<table class="msgtab" id="fetchresult">

      <tr  class="trow" onclick=location.href="conversation.php?value='.$id.'">

     <td class="msg-col-1"><img src="images/profilepic/'.$r["profile_pic"].'" alt="Avatar" class="circlemsg"></td>

     <td class="msg-col-2">
        <h5 class="msgheading">'.$nm.'</h5>
        <p class="msgcontent">'.$about.'</p>

        </td>
        </tr>';

   }


$output .= '</table>';
echo $output;

}

在此之后,我的整行现在都可以点击了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多