回声JS onclick填充功能AJAX不起作用

时间:2019-03-21 17:53:43

标签: javascript php mysql ajax

我有一个带有AJAX,MySQL,PHP和JS的搜索栏。搜索栏在列表中的div中提供实时搜索结果,并在这方面起作用。

我的问题是,单击div列表中的实时搜索结果不会填充搜索栏的输入。没有任何反应,除非我单击它,否则列表将保持打开状态。我曾经在旧代码中使用过它,当您单击任何结果时,它就会填满搜索栏的输入,但是自从我重写了整个代码后,我一直无法弄清楚为什么onclick和{{1} }功能现在无法使用。

如何解决此代码,以便当用户单击实时搜索结果列表中的结果之一时,它会填充搜索栏的输入内容?

这是我尝试过的内容,目前作为以下文件中的代码:

index.php

fill

script.js

<form>  
 <input type="text" id="search" class="search" data-js="form-text" 
 placeholder="Search Over 100+ Resources..." autocomplete="off">
 <button type="submit" class="Button" value="Submit"><i class="fa fa- 
 search"></i></button>
 <div id="display"></div>
 <div id="backspace" style="display:none"></div>
</form>

ajax.php

//Getting value from "ajax.php".
function fill(Value) {
//Assigning value to "search" div in "index.php" file.
$('#search').val(Value);
//Hiding "display" div in "index.php" file.
$('#display').hide();
}
$(document).ready(function() {
//On pressing a key on "Search box" in "indexd.php" file. This function will 
be called.
$("#search").keyup(function() {
//Assigning search box value to javascript variable named as "name".
$('#display').hide();
$('#backspace').css("display", "none");
var name = $('#search').val();
//Validating, if "name" is empty.
if (name == "") {
   //Assigning empty value to "display" div in "index.php" file.
   $('#backspace').css("display", "block");
}
//If name is not empty.
else {
    //AJAX is called.
    $.ajax({
        //AJAX type is "GET".
        type: "GET",
        //Data will be sent to "ajax.php".
        url: "ajax.php",
        //Data, that will be sent to "ajax.php".
        data: {
            //Assigning value of "name" into "search" variable.
            search: name
        },
        //If result found, this funtion will be called.
        success: function(html) {
            //Assigning result to "display" div in "index.php" file.
            $("#display").html(html).show();
        }
    });
   }
 });
});

1 个答案:

答案 0 :(得分:7)

对于JavaScript,您可以使用`符号代替单引号和双引号。

在这里,您应该像这样更新ajax.php文件中的第16行。

echo "<li onclick='fill(`".$Result['Name']."`)'>".$Result['Name']." 

完整代码 ajax.php文件

<?php
//Including Database configuration file.
include "db.php";
//Getting value of "search" variable from "script.js".
if (isset($_GET['search'])) {
//Search box value assigning to $Name variable.
$Name = $_GET['search'];
//Search query.
$Query = "SELECT Name FROM search WHERE Name LIKE '$Name%' LIMIT 5";
//Query execution
$ExecQuery = MySQLi_query($con, $Query);
//Creating unordered list to display result.
    if ($ExecQuery->num_rows > 0) {
          echo "<ul>";
          while ($Result = MySQLi_fetch_array($ExecQuery)) {
            echo "<li onclick='fill(`".$Result['Name']."`)'>".$Result['Name']." 
     </li>";
     }
    echo "</ul>";
   }
  }
 die();
?>

JS代码。

//Getting value from "ajax.php".
function fill(Value) {
//Assigning value to "search" div in "index.php" file.
$('#search').val(Value);
//Hiding "display" div in "index.php" file.
$('#display').hide();
}
$(document).ready(function() {
//On pressing a key on "Search box" in "indexd.php" file. This function will be called.
$("#search").keyup(function() {
//Assigning search box value to javascript variable named as "name".
$('#display').hide();
$('#backspace').css("display", "none");
var name = $('#search').val();
//Validating, if "name" is empty.
if (name == "") {
   //Assigning empty value to "display" div in "index.php" file.
   $('#backspace').css("display", "block");
}
//If name is not empty.
else {
    //AJAX is called.
    $.ajax({
        //AJAX type is "GET".
        type: "GET",
        //Data will be sent to "ajax.php".
        url: "ajax.php",
        //Data, that will be sent to "ajax.php".
        data: {
            //Assigning value of "name" into "search" variable.
            search: name
        },
        //If result found, this funtion will be called.
        success: function(html) {
          if (html == '<ul><li>No Result Found!</li></ul>') {
             $('#no-results').css("display", "block");
          }else{
             //Assigning result to "display" div in "index.php" file.
             $("#display").html(html).show();
           }
        }
    });
   }
 });
});