【问题标题】:Auto suggest with two input boxes使用两个输入框自动建议
【发布时间】: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"> &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 有一个自动完成小部件,它支持远程数据源。
  • 好的,为此欢呼。方向错了。
  • @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


【解决方案1】:

我建议使用具有自动完成小部件的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2015-02-13
    • 2018-08-22
    • 2011-04-08
    • 2012-08-24
    • 1970-01-01
    相关资源
    最近更新 更多