【问题标题】:Auto fill from search result with jquery使用 jquery 从搜索结果中自动填充
【发布时间】:2015-10-14 22:25:19
【问题描述】:

您好,我有以下代码使用用户 ID、用户名、电子邮件填充搜索结果

我想用适当的 ID 用户点击自动填充 id= id 的字段

    <?php 

    include '../db/connect6.php';


if(isset($_POST['searchVal'])) {
  $searchq = $_POST['searchVal'];
  $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);

    $query = mysql_query("SELECT * FROM oz2ts_users WHERE oz2ts_users.id LIKE  '%$searchq%' OR oz2ts_users.name LIKE '%$searchq%'") or die("Could not  search"); 
    $count = mysql_num_rows($query);
    if($count == 0){
     $output = 'There is no result to show!'; 
    } else{ 
     while($row = mysql_fetch_array ($query)) {
        $id = $row['id'];
        $name = $row['name'];
        $username = $row['username'];    

    $output .= '<div><a class="resultItem" data-id="' . $id . '">'   
     . $name . ' '.$username.'</a></div>';   

   }            
 }

 }
echo($output);
?>

【问题讨论】:

    标签: php jquery mysqli


    【解决方案1】:

    只需执行一个针对该字段的 .onclick() 事件。示例代码如下。

    <form>
       <input id="display-id" type="text" />
    </form>
    
    <script>
    $(document).ready(function(){
        $('.resultItem').click(function(){
             var resultId = $(this).data('id');
             $('#display-id').val(resultId);
        });    
    });
    </script>
    

    【讨论】:

    • 谢谢蒂姆。我试过了,但它不起作用。我有错误TypeError: $(...).onclick is not a function 可能来自`$('.resultItem').onclick(function(){` 错误显示表单加载时单击什么都不做
    • 糟糕,我这样做的时候一定是睡着了。我编辑了上面的代码以更正它。简而言之,“onclick”(正如我以前所拥有的)是纯 javascript,但“.click()”(就像我现在所拥有的那样)是 jQuery 方式:D
    猜你喜欢
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    相关资源
    最近更新 更多