我遇到了和这个问题完全一样的问题:
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"> </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"> </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/