javascript - 自动建议两个输入框

标签 javascript html forms autosuggest

我遇到了和这个问题完全一样的问题:
auto suggest php ajax with two input boxes not working with the given sample code

然而,在尝试这些建议时,我仍然遇到同样的问题。
我有两个输入框,我想要一个来自数据库的自动建议。
然而,无论输入哪个字段,自动建议只会出现在其中一个框的下方。
这就是我在顶部链接的问题中提出建议后得到的结果。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
function suggest(inputString){
    if(inputString.length == 0) {
        $('#suggestions').fadeOut();
    } else {
    $('#customer').addClass('load');
        $.post("/templates/autosuggest/autosuggest.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').fadeIn();
                $('#suggestionsList').html(data);
                $('#customer').removeClass('load');
            }
        });
        }
    }

function fill(thisValue) {      
    $('#customer').val(thisValue);
    setTimeout("$('#suggestions').fadeOut();", 10);
}
</script>

<script>
function suggest(inputString){
if(inputString.length == 0) {
    $('#suggestionssearch').fadeOut();
} else {
$('#customersearch').addClass('load');
    $.post("/templates/autosuggest/autosuggest.php", {queryString: ""+inputString+""}, function(data){
        if(data.length >0) {
            $('#suggestionssearch').fadeIn();
            $('#suggestionsListsearch').html(data);
            $('#customersearch').removeClass('load');
        }
    });
    }
}

function fill(thisValue) {      
$('#customersearch').val(thisValue);
setTimeout("$('#suggestionssearch').fadeOut();", 10);
}
</script>

还有我的输入:

<input type="text" name="Customer" value="" id="customer" onkeyup="suggest(this.value);" onblur="fill();" class="" style="height:14px; margin-top:2px;" placeholder="Search Customers" autocomplete="off">
<input type="submit">
<div id="suggestcontainer" style="margin-left:2px;">
  <div class="suggestionsBox" id="suggestions" style="display: none;"> 
    <div class="suggestionList" id="suggestionsList"> &nbsp; </div>
  </div>
</div>


<input type="text" name="Customer" value="" id="customersearch" onkeyup="suggest(this.value);" onblur="fill();" class="" style="width:90px; height:14px; margin-top:2px;" placeholder="Customer" autocomplete="off" required="required"/>
<input type="submit" style="float:right;">
<div id="suggestcontainersearch">
  <div class="suggestionsBoxsearch" id="suggestionssearch" style="display: none;"> 
    <div class="suggestionListsearch" id="suggestionsListsearch"> &nbsp; </div>
  </div>
</div>

我已经用我的脚本尝试过这个作为测试:

<script>
 function suggest(inputString){
    if(inputString.length == 0) {
        $('#suggestions').fadeOut();
                    $('#suggestionssearch').fadeOut();
    } else {
    $('#customer').addClass('load');
            $('#customersearch').addClass('load');
        $.post("/templates/autosuggest/autosuggest.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').fadeIn();
                $('#suggestionsList').html(data);
                $('#customer').removeClass('load');
                $('#suggestionssearch').fadeIn();
                $('#suggestionsListsearch').html(data);
                $('#customersearch').removeClass('load');
            }
        });
        }
    }

function fill(thisValue) {      
    $('#customer').val(thisValue);
    setTimeout("$('#suggestions').fadeOut();", 10);


    $('#customersearch').val(thisValue);
    setTimeout("$('#suggestionssearch').fadeOut();", 10);
}
</script>

这在某种意义上是有效的,如果我输入一个然后两个都出现,显然不是我需要的但表明它看到了它们。似乎它不喜欢有两个脚本。

任何帮助都会很棒。 提前给大家打气。

此外,如果有人知道我如何使用键盘向上和向下按钮滚动浏览自动建议的结果,那将是锦上添花!!

最佳答案

我建议使用 jQuery UI它有一个自动完成的小部件。

$( "#birds" ).autocomplete({
    source: "search.php",
    minLength: 2,
    select: function( event, ui ) {
        log( ui.item ?
            "Selected: " + ui.item.value + " aka " + ui.item.id :
            "Nothing selected, input was " + this.value );
    }
});

回复评论

要自定义自动完成建议的出现顺序,您可以将 SQL 查询更改为使用 order by case .下面的查询将返回以 $search 开头的所有结果,然后再返回仅包含它的其他结果。

SELECT Customer 
FROM Customers 
WHERE Customer LIKE '%$search%' 
ORDER BY CASE 
    WHEN Customer LIKE '$search%' THEN 1 
    ELSE 2 
END

关于javascript - 自动建议两个输入框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14269801/

相关文章:

javascript - 使用 JavaScript 和 Bootstrap 分段按钮更改表单操作和下拉菜单中的输入值

javascript - 识别div内的字符并添加类javascript

javascript - VueJS : use computed "set/get" with Vue. 可拖动和 VueX

Javascript 检查空字段,安全吗?

javascript - 如何调用js获取合适的元素

html - Tweet Link 和 Facebook Like & Send 按钮在 Chrome、Safari 和 Firefox 中是水平的,但在 Internet Explorer 8 中不是

jquery - div 的条纹类

html - 对 input[type=submit] 应用某种效果

javascript - 如何根据子值获取值

javascript - 将 html 表单的多个输入值从一个输入字段传递给 JavaScript 事件函数