【问题标题】:use auto complete in multiple input fields在多个输入字段中使用自动完成
【发布时间】:2020-10-15 14:07:38
【问题描述】:

希望所有人都做得很好。好吧,我遇到了一个问题,我希望在相同表单的 2 个输入字段中显示数据库中的城市名称,但每次我在两个输入字段上选择时,都会选择相同的值。

如果有人可以查看我的代码并帮助我解决这个问题,我们将不胜感激。

这是我的 search.php

require_once 'includes/config.php';

  if (isset($_POST['query'])) {
    $inpText = $_POST['query'];
    $sql = 'SELECT * FROM pt_cities WHERE cityName LIKE :cityName';
    $stmt = $db->prepare($sql);
    $stmt->execute(['cityName' => '%'.$inpText.'%']);
    $result = $stmt->fetchAll();

    if ($result) {
      foreach ($result as $row) {
        echo '<a href="#" class="list-group-item list-group-item-action border-1">'.$row['cityName'].' ('.$row['code'].')</a>';
      }
    } else {
      echo '<p class="list-group-item border-1">No Record</p>';
    }
  }

这是我的 js

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
            <script>
            $(document).ready(function () {
  // Send Search Text to the server
  $(".search").keyup(function () {
    let searchText = $(this).val();
    if (searchText != "") {
      $.ajax({
        url: "search.php",
        method: "post",
        data: {
          query: searchText,
        },
        success: function (response) {
          $(".show-list").html(response);
        },
      });
    } else {
      $(".show-list").html("");
    }
  });
  // Set searched text in input field on click of search button
  $(document).on("click", "a", function () {
    $(".search").val($(this).text());
    $(".show-list").html("");
  });
});
    </script>

我的表单看起来像这样

<div class="col-sm-12">
                    <label class="form-label-outside">From</label>
                    <div class="form-wrap form-wrap-inline">
                       <input class="form-input search" name="from" type="text" >
                       <div class="list-group show-list">        
                    </div>
                    </div>
                  </div>
                  <div class="col-sm-12">
                    <label class="form-label-outside">To</label>
                    <div class="form-wrap form-wrap-inline">
                      <input class="form-input search"  name="to" type="text" >
                       <div class="list-group show-list">        
                    </div>
                    </div>
                  </div>

提前感谢您的帮助

【问题讨论】:

    标签: javascript jquery autocomplete


    【解决方案1】:

    欢迎!如果我理解正确,问题是您的一个输入的自动完成结果出现在两个具有show-list 类的 div 中。问题是:

    $(".show-list").html(response)
    

    由于".show-list" 选择了具有"show-list" 类的两个div,因此编辑了两个div 的HTML,即使您只关注一个输入的值。您必须以某种方式区分两个 show-list div 及其各自的输入。为什么不给每个元素一个 ID 并制作两个独立的 jQuery 事件监听器?

    ...
    <input id="from-input" class="form-input search" name="from" type="text">
    <div id="from-show-list" class="list-group show-list"></div>
    ...
    <input id="to-input" class="form-input search"  name="to" type="text">
    <div id="to-show-list" class="list-group show-list"></div>
    
    $("#from-input").keyup(function () {
        let searchText = $(this).val();
        if (searchText != "") {
          $.ajax({
            url: "search.php",
            method: "post",
            data: {
              query: searchText,
            },
            success: function (response) {
              $("#from-show-list").html(response);
            },
          });
        } else {
          $("#from-show-list").html("");
        }
      });
    
    $("#to-input").keyup(function () {
        let searchText = $(this).val();
        if (searchText != "") {
          $.ajax({
            url: "search.php",
            method: "post",
            data: {
              query: searchText,
            },
            success: function (response) {
              $("#to-show-list").html(response);
            },
          });
        } else {
          $("#to-show-list").html("");
        }
      });
    

    【讨论】:

    • 谢谢伙计,但现在结果没有被点击
    • 抱歉,没有点击结果是什么意思?由于这两个函数不起作用,或者show-list div 中生成的结果现在不起作用?
    • 正在获取结果,但是当我点击它时,输入框中没有输入任何内容
    • 在这些更改之前单击结果是否按预期工作?如果没有,这些a 元素可能会在声明事件侦听器后动态生成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多