PHP-Ajax:实时搜索,下拉列表并分配值

我正在使用html输入“搜索”实现实时搜索,结果应显示在其下方,以便用户可以选择正确的名称。一旦客户选择名称,它将所选名称的ID分配给其他隐藏的输入“id”。以下是我的代码详细信息:


索引.php


<?php


?>


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>PHP Live MySQL Database Search</title>


<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<script>

$(document).ready(function(){

  $("#search").keyup(function(){

    var txt = $(this).val();

    var resultDropdown = $(this).siblings(".result");

    var personname = "";

    var personID = "";

    if (txt !='')

    {

        $.ajax

        (

        {

            type:"post",    //submit method

            url: "search.php",  //url to sumitted data To

            data: {name : txt}, //Data to be submitted

            cache: false,

            dataType: 'json',

            //action on successful post request

            success: function(data)

            {

                //process JSON

                $.each(data.names, function(idx, name){


                    var person = "<p>" + name.name +"</p>";

                    resultDropdown.html(person);

                });


            },

        }

        )

    }

    else

    {

        resultDropdown.empty();

    }

  });


   // Set search input value on click of result item



});

</script>


</head>

<body>

<form method="post" action="result.php">

    <div class="search-box">


        <input type="text" id="search" autocomplete="off" name="spousename" placeholder="search spouse" />

        <input type="hidden" id="id" autocomplete="off" name="spouseid" placeholder="search spouse" />

        <input class="w3-button w3-block w3-green w3-section w3-padding" type="submit" name="access"><b>Login</b></button>

        <div class="result"></div>

    </div>

</form>

</body>

</html>

从搜索返回的数据.php


{"names":[{"name":" xxxxxx","id":3},{"name":"yyyy","id":6},{"name":"zzzz","id":5}]}

我面临的问题: 1- 结果滴落.html(人): 只显示 json 上的最后一项


2-如何在点击时分配ID


开心每一天1111
浏览 107回答 2
2回答

慕妹3242003

将 html 与变量一起评估,然后在循环后显示它var resultDropdown = $(".result");success: function(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; var person = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //process JSON&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(data.names, function(idx, name){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person += '<p class="name" data-id="'+ name.id +'">' + name.name +'</p>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; resultDropdown.html(person);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }// Get the id of the clicked person&nbsp; &nbsp; &nbsp; &nbsp; $(document).on('click', '.name', function(){&nbsp; &nbsp; var id = $(this).attr('data-id');})

慕桂英3389331

这是完整的代码,如果一些<?php?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>PHP Live MySQL Database Search</title><style type="text/css">&nbsp; &nbsp; body{&nbsp; &nbsp; &nbsp; &nbsp; font-family: Arail, sans-serif;&nbsp; &nbsp; }&nbsp; &nbsp; /* Formatting search box */&nbsp; &nbsp; .search-box{&nbsp; &nbsp; &nbsp; &nbsp; width: 300px;&nbsp; &nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; &nbsp; font-size: 14px;&nbsp; &nbsp; }&nbsp; &nbsp; .search-box input[type="text"]{&nbsp; &nbsp; &nbsp; &nbsp; height: 32px;&nbsp; &nbsp; &nbsp; &nbsp; padding: 5px 10px;&nbsp; &nbsp; &nbsp; &nbsp; border: 1px solid #CCCCCC;&nbsp; &nbsp; &nbsp; &nbsp; font-size: 14px;&nbsp; &nbsp; }&nbsp; &nbsp; .result{&nbsp; &nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; z-index: 999;&nbsp; &nbsp; &nbsp; &nbsp; top: 100%;&nbsp; &nbsp; &nbsp; &nbsp; left: 0;&nbsp; &nbsp; }&nbsp; &nbsp; .search-box input[type="text"], .result{&nbsp; &nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; &nbsp; box-sizing: border-box;&nbsp; &nbsp; }&nbsp; &nbsp; /* Formatting result items */&nbsp; &nbsp; .result p{&nbsp; &nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; &nbsp; &nbsp; padding: 7px 10px;&nbsp; &nbsp; &nbsp; &nbsp; border: 1px solid #CCCCCC;&nbsp; &nbsp; &nbsp; &nbsp; border-top: none;&nbsp; &nbsp; &nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; }&nbsp; &nbsp; .result p:hover{&nbsp; &nbsp; &nbsp; &nbsp; background: #f2f2f2;&nbsp; &nbsp; }</style><script src="https://code.jquery.com/jquery-1.12.4.min.js"></script><script>$(document).ready(function(){&nbsp; $("#search").keyup(function(){&nbsp; &nbsp; var txt = $(this).val();&nbsp; &nbsp; var resultDropdown = $(".result");&nbsp; &nbsp; var person = "";&nbsp; &nbsp; if (txt !='')&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type:"post",&nbsp; &nbsp; //submit method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "search.php",&nbsp; //url to sumitted data To&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {name : txt}, //Data to be submitted&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //action on successful post request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //process JSON&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(data.names, function(idx, name){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person += '<p data-id="'+ name.id +'">' + name.name +'</p>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultDropdown.html(person);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; resultDropdown.empty();&nbsp; &nbsp; }&nbsp; });&nbsp; &nbsp;// Get the id of the clicked person&nbsp; &nbsp; &nbsp; &nbsp; $(document).on("click", ".result p", function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //assign the value of person name to search input&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $(this).parents(".search-box").find('#search').val($(this).text());&nbsp; &nbsp; &nbsp; &nbsp; //get the id&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var id = $(this).attr('data-id');&nbsp; &nbsp; &nbsp; &nbsp; //set input id "id" value&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$("#id").val(id);&nbsp; &nbsp; &nbsp; &nbsp; //clear search data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$(this).parent(".result").empty();&nbsp; &nbsp; })});</script></head><body><form method="post" action="result.php">&nbsp; &nbsp; <div class="search-box">&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="search" autocomplete="off" name="spousename" placeholder="search spouse" />&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" id="id" autocomplete="off" name="spouseid" placeholder="search spouse" />&nbsp; &nbsp; &nbsp; &nbsp; <input class="w3-button w3-block w3-green w3-section w3-padding" type="submit" name="access"><b>Submit</b></button>&nbsp; &nbsp; &nbsp; &nbsp; <div class="result"></div>&nbsp; &nbsp; </div></form></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP