Ajax自动完成以相同的值填充两个输入字段,尽管我用不同的逻辑分别实现了这两个功能

问题描述 投票:1回答:2

我有2个输入字段

  1. 学生姓名输入字段
  2. 组名输入字段

我实现了2种不同的Ajax逻辑,以分别在不同标签中的文本框中获取学生和组的列表以实现自动完成。

但是当我从学生列表或小组列表的任何自动完成值中单击一个值时,两个输入字段都被所选值填满

图片:Group List

图片:Both fields are filling up

用于获取学生的代码(JavaScript):

<script>
        $(document).ready(function()
        {
            $("#studentname").keyup(function()
            {
                var studentName = $(this).val();
                if(studentName != '')
                {
                    $.ajax({
                       url: "SearchStudent.php",
                       method: "POST",
                       data:{studentName:studentName},
                       success: function(data)
                       {
                           $('#students').fadeIn();
                           $('#students').html(data);
                       }
                    });
                }
                else
                {
                    $('#students').fadeOut();
                    $('#students').html("");
                }
            });
            $(document).on('click','li',function()
            {
               $('#studentname').val($(this).text());
               $('#students').fadeOut();
            });
        });
    </script>

获取组代​​码(JavaScript):

<script>
        $(document).ready(function()
        {
            $("#groupname").keyup(function()
            {
                var groupName = $(this).val();
                if(groupName != '')
                {
                    $.ajax({
                       url: "SearchGroup.php",
                       method: "POST",
                       data:{groupName:groupName},
                       success: function(Groupdata)
                       {
                           $('#groups').fadeIn();
                           $('#groups').html(Groupdata);
                       }
                    });
                }
                else
                {
                    $('#groups').fadeOut();
                    $('#groups').html("");
                }
            });
            $(document).on('click','li',function()
            {
               $('#groupname').val($(this).text());
               $('#groups').fadeOut();
            });
        });
    </script>

PHP代码(SearchStudent.php):

<?php
session_start();
require_once "../../config/connection.php";
if(isset($_SESSION['admin']) && $_POST['studentName'])
{
    $StudentName = $_POST['studentName'];

$output = '';
$searchStudent = "SELECT StudentName FROM student_details WHERE StudentName LIKE '%$StudentName%' LIMIT 10";
$searchStudentFire = mysqli_query($conn, $searchStudent);
$output = "<ul style='list-style-type: none;'>";
if(mysqli_num_rows($searchStudentFire) > 0)
{
    while($StudentList = mysqli_fetch_assoc($searchStudentFire))
    {
        $output.= "<li style='font-weight:bold; border: 1px; padding: 10px; box-shadow: 5px 10px 18px #888888;'>".$StudentList['StudentName']."</li>";
    }
}
else
{
    $output.= "<li>No Student Found</li>";
}
$output .= "</ul>";
echo $output;
}
else
{
    echo "Unautorizarion Error !!";
}
?>

PHP代码(SearchGroup.php):

<?php
session_start();
require_once "../../config/connection.php";
if(isset($_SESSION['admin']) && $_POST['groupName'])
{
    $GroupName = $_POST['groupName'];

$output = '';
$searchGroup = "SELECT GroupName FROM groups WHERE GroupName LIKE '%$GroupName%' LIMIT 10";
$searchGroupFire = mysqli_query($conn, $searchGroup);
$output = "<ul style='list-style-type: none;'>";
if(mysqli_num_rows($searchGroupFire) > 0)
{
    while($GroupList = mysqli_fetch_assoc($searchGroupFire))
    {
        $output.= "<li style='font-weight:bold; border: 1px; padding: 10px; box-shadow: 5px 10px 18px #888888;'>".$GroupList['GroupName']."</li>";
    }
}
else
{
    $output.= "<li>No Group Found</li>";
}
$output .= "</ul>";
echo $output;
}
else
{
    echo "Unautorizarion Error !!";
}
?>

输入字段和自动完成区域:

<input autocomplete="off" class="input--style-5" type="text" name="studentname" id="studentname"> <!--Student Field-->
<div style="" id="students" name="students"></div> <!--Auto Complete Area Div-->

<input autocomplete="off" class="input--style-5" type="text" id="groupname" name="groupname"><!--Group Field-->
<div id="groups" name="groups"></div><!--Autocomplete area-->
javascript php jquery ajax autocomplete
2个回答
0
投票

以下两个块是您的问题,它们都响应对任何li的单击。

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


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

您需要调整选择器以将各个列表作为目标,如下所示:

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


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

或类似这样:

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


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

0
投票

您的选择器在您的“开启”功能中不够具体。试试:

© www.soinside.com 2019 - 2024. All rights reserved.