javascript - 我无法选择搜索项目

标签 javascript php ajax bootstrap-4

我正在使用 php、mysql、js 和 ajax 创建搜索功能。但我无法选择搜索项目。我正在尝试选择建议的词。有加载建议,但我无法选择它们。这是我创建的代码。请

这是索引页。在索引页脚本部分是错误的或不起作用的淡出效果的最后一部分。

<head>
    <title>2nd year group project</title>
    <meta charset = "utf-8">
    <metaname="viewport" content="width=device-width, initial-scale=1">

    <!--this is for link css file-->
    <link rel="stylesheet" type="text/css" href="css/FindAProffesional.css">

    <!--//this is for link icons for site-->
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">

    <!--//this is for link bootstrap to site-->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>  
<body>
<div style="width:500px;">
<input type="text" name="country" id="country" class="form-control" placeholder="Enter city name">
</div>
<div id="countryList"></div>
<script>
    $(document).ready(function(){
        $('#country').keyup(function(){
        var query = $(this).val();
        if(query!='')
        {
            $.ajax({
            url:"search.php",
            method:"POST",
            data:{query:query},
            success:function(data)
        {
        $('#countryList').fadeIn();
        $('#countryList').html(data);
        }
        });
        }   
    });

        $document().on('click','li',function(){
        $('#country').val($(this).text());
        $('#countryList').fadeOut();
        });
    });
</script>
</body>

这是serch.php页面

<?php
    require('dbcon.php');
    if(isset($_POST["query"]))
    {
        $output='';
        $query ="SELECT DISTINCT city FROM architect WHERE city LIKE '%".$_POST["query"]."%'";
        $result = mysqli_query($conn,$query);
        $output = '<ul class="list-unstyled">';
        if(mysqli_num_rows($result) > 0)
        {
            while($row = mysqli_fetch_array($result))
            {
                $output .='<li>'.$row["city"].'</li>';
            }
        }
        else
        {
            $output .='<li>City not Found</li>';
        }
        $output .='</ul>';
        echo $output;

    }
?>

最佳答案

替换

$document().on('click','li',function(){
        $('#country').val($(this).text());
        $('#countryList').fadeOut();
        });

$(function(){
  $('#countryList').on('click','li',function(){
    $('#country').val($(this).text());
    $('#countryList').fadeOut();
  });
});

关于javascript - 我无法选择搜索项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46257460/

相关文章:

javascript - 在 FabricJS 中绘制后无法选择对象

php - 如何使用 mysqli 准备好的语句更新数据库?

php - 从 php 触发 bash 脚本时,服务未执行

java - 如何防止单击 <h :commandLInk> 时页面刷新

javascript - JavaScript 中函数参数的范围

c# - Odata 使用 Knockout Js 路由和访问它

javascript - Internet Explorer 11 中的 SCRIPT438 错误

javascript - 反汇编的 javascript jit 代码调用未知函数

php - C 与 php/asp 连接

java - Ajax post无法找到URL,这导致java类带有注释。给出错误 404