【问题标题】:How to clear input field whitespace in live ajax search after I click on a result单击结果后如何在实时 ajax 搜索中清除输入字段空白
【发布时间】:2017-06-05 16:52:01
【问题描述】:

我有一个使用 PHP jQuery 和 ajax 的简单 ajax 实时搜索脚本。这一切都很好,但输入框没有完全清除,所以结果框不会消失,除非我手动清除它..

JS -

<script type="text/javascript">
        $(document).ready(function(){
            $('.search-box input[type="text"]').on("keyup input", function(){
                /* Get input value on change */
                var inputVal = $(this).val();
                var resultDropdown = $(this).siblings(".result");
                if(inputVal.length){
                    $.get("backend-search.php", {term: inputVal}).done(function(data){
                        // Display the returned data in browser
                        resultDropdown.html(data);
                    });

                } else{
                    resultDropdown.empty();
                }
            });

            // Set search input value on click of result item
            $(document).on("click", ".result", function(){
                $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
                $(this).parent(".result").empty();

            });
        });
    </script>

PHP -

$user = $_GET['user'];

$term = mysqli_real_escape_string($conn, $_REQUEST['term']);
if(isset($term)){
// Attempt select query execution
$sql = "SELECT * FROM prospects WHERE dealer_name LIKE '" . $term . "%'";
if($result = mysqli_query($conn, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $dealer_name = $row['dealer_name'];
            echo "<form method='post' class='editForm'>
             <input type='hidden' name='dealer' value='$dealer_name'/>
                        <input type='submit' id='subDealer' value='$dealer_name'/>
                    </form>";
        }
        // Close result set
    } else{
        echo "<p>No matches found</p>";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}

HTML -

 <div class="search-box">
     <input type="text" id="searchBox" autocomplete="off" placeholder="Search dealer..." />
     <div class="result"></div>
 </div>

就像它说的那样,这一切都有效,但输入字段 (#searchBox) 中有空白空格,这将不允许清除 .result..

谢谢!

【问题讨论】:

  • 哪里涉及到php?
  • 对不起,我会尽快添加它
  • 我可以得到哪里是你的问题,如果if(inputVal.length){在这?还是在点击事件$(this).parent(".result").empty();
  • 如果你想在你的值的开头和结尾删除空格,你可以使用 trim(), $(this).val().trim( )
  • @Jeff 是的

标签: javascript jquery ajax


【解决方案1】:

试试下面的sn-p

// Set search input value on click of result item
$(document).on("click", ".result", function(){
    $(this).parents(".search-box").find('input[type="text"]').val($(this).text().trim());
});


$(document).on('click', 'input[type=submit]', function() {
   // This is NOT the best practice. But it works. Try to change your implementation to a better one!
   setTimeout(function() {
       $('.result').empty();
   }, 1000);
   return true;
}

【讨论】:

  • 这解决了我的问题,但现在得到“表单提交被取消,因为表单未连接”
  • 好吧,在我看来很难调试代码:P。所以告诉我,它以前工作过吗?并且看起来表单的action 属性丢失了。
  • 对不起,除了清除输入字段外,一切正常。似乎当我更改 $(this).parent(".result").empty();对此 - $(this).empty();它以某种方式断开表单
  • 我理解了这个问题。现在这里发生的是在发送form 帖子之前,.result div 被清除。在它没有发生之前,因为它实际上并没有清空.result div。现在告诉我,你什么时候要清空 div,为什么要清空 div?
  • 啊,对可怕的解释感到抱歉..是的,我想单击结果中的表单元素然后清除结果..原因是结果是下拉列表,一旦我找到结果我想要并单击它我有 php 加载,这意味着在我进行另一次搜索之前我不再需要查看结果。你是正确的,结果似乎在单击按钮后加载表单之前被删除了。
【解决方案2】:

使用 jQuery trim() 函数

 <script type="text/javascript">
            $(document).ready(function(){
                $('.search-box input[type="text"]').on("keyup input", function(){
                    /* Get input value on change */
                    var inputVal1 = $(this).val();
                    var inputVal = $.trim(inputVal1);
                    var resultDropdown = $(this).siblings(".result");
                    if(inputVal.length){
                        $.get("backend-search.php", {term: inputVal}).done(function(data){
                            // Display the returned data in browser
                            resultDropdown.html(data);
                        });

                    } else{
                        resultDropdown.empty();
                    }
                });

                // Set search input value on click of result item
                $(document).on("click", ".result", function(){
                    $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
                    $(this).parent(".result").empty();

                });
            });
        </script>

【讨论】:

  • 谢谢,但仍然得到相同的结果,字段中有一堆空白
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2021-10-16
相关资源
最近更新 更多