【发布时间】:2013-01-11 00:32:43
【问题描述】:
我和这个问题有完全相同的问题:
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 有一个自动完成小部件,它支持远程数据源。
-
好的,为此欢呼。方向错了。
-
@atomman 我现在只需要知道如何对建议结果的顺序进行排序,这样如果输入字母“M”,那么所有以“M”开头的结果都会首先出现。目前,它会在单词中的任何位置找到所有带有字母“M”的单词,但从第一个字母开始按字母顺序排列。如果这有任何意义...已尝试在数据库中执行 order by 选择但无济于事。
-
我正在使用的代码。脚本:
$(function() { $( "#customersearch" ).autocomplete({ source: "/templates/autosuggest/customersuggest.php", select: function( event, ui ) { var selectedObj = ui.item; } }); });和数据库选择:$search = $_GET['term']; $letter = substr($search, 0, 1); $req = "SELECT Customer FROM Customers WHERE Customer LIKE '%$search%' ORDER BY Customer LIKE '%$letter%'"; -
SELECT Customer FROM Customers WHERE Customer LIKE '%$search%' ORDER BY CASE WHEN Customer LIKE '$search%' THEN 1 ELSE 2 END可能是这样的,不过没有经过测试。
标签: javascript html forms autosuggest