自动建议两个输入框

J A*_*len 6 html javascript forms autosuggest

我有与这个问题完全相同的问题:
自动建议php ajax有两个输入框不能使用给定的示例代码

然而,在尝试建议时,我仍然有同样的问题.
我有两个输入框,我想从db中自动建议.
然而,自动建议只出现在其中一个框下方,无论输入哪个字段.
这是我在最高层链接的问题中的建议后最终得到的结果.

<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>
Run Code Online (Sandbox Code Playgroud)

我的意见:

<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>
Run Code Online (Sandbox Code Playgroud)

我用我的脚本作为测试尝试了这个:

<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>
Run Code Online (Sandbox Code Playgroud)

这是有效的,如果我输入一个然后两个出现,显然不是我需要的,但表明它看到了它们.好像它不喜欢有两个脚本.

任何帮助都会很棒.提前为每个人喝彩.

此外,如果有人知道我怎么能用滚动键盘向上和向下按钮滚动自动建议结果,这将是锦上添花!

ato*_*man 2

我建议使用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 );
    }
});
Run Code Online (Sandbox Code Playgroud)

回复评论

要自定义自动完成建议的顺序,您可以更改 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
Run Code Online (Sandbox Code Playgroud)