亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊時自動完成更新第二個輸入框上的相同值

單擊時自動完成更新第二個輸入框上的相同值

qq_遁去的一_1 2023-05-11 16:02:22
請有人幫我解決這個問題,因為大多數事情看起來都不錯,比如從數據庫中獲取匹配結果,但是當我在兩個輸入框中單擊值時,會添加相同的自動完成值。有人可以幫我解決這個問題嗎?這是我的 html:<div class="col-sm-12">    <label class="form-label-outside">From</label>        <div class="form-wrap form-wrap-inline">        <input id="from-input" class="form-input" name="from" type="text">        <div id="from-show-list" class="list-group"></div>    </div></div><div class="col-sm-12">    <label class="form-label-outside">To</label>    <div class="form-wrap form-wrap-inline">        <input id="to-input" class="form-input"  name="to" type="text">        <div id="to-show-list" class="list-group"></div>    </div></div>我的 js<script src="js/jquery.min.js"></script><script>    $(document).ready(function() {        $("#from-input").keyup(function() {            let searchText = $(this).val();            if (searchText != "") {                $.ajax({                    url: "airports.php",                    method: "post",                    data: {                        query: searchText,                    },                    success: function(response) {                        $("#from-show-list").html(response);                    },                });            } else {                $("#from-show-list").html("");            }        });        // Set searched text in input field on click of search button        $(document).on("click", "a", function() {            $("#from-input").val($(this).text());            $("#from-show-list").html("");        });    });
查看完整描述

1 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

問題出現是由于鏈接上的點擊功能。通過指定包含這些鏈接的 div 的 ID 來定義兩個單獨的鏈接組。


    // Set searched text in input field on click of search button

    $(document).on("click", "#from-show-list a", function() {

        $("#from-input").val($(this).text());

        $("#from-show-list").html("");

    });


    // Set searched text in input field on click of search button

    $(document).on("click", "#to-show-list a", function() {

        $("#to-input").val($(this).text());

        $("#to-show-list").html("");

    });

像這樣使用 css 將最大高度應用于結果 div。


<div id="from-show-list" class="list-group" style="max-height: 100px;  overflow: auto;"></div>

<div id="to-show-list" class="list-group" style="max-height: 100px;  overflow: auto;"></div>



查看完整回答
反對 回復 2023-05-11
  • 1 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號