简体   繁体   English

自动完成,多个字段,PHP MySql

[英]Autocomplete, multiple fields, PHP MySql

I'm sure this is simple but driving me mad... 我敢肯定这很简单,但是让我发疯了...

I have the following Autocomplete script: 我有以下自动完成脚本:

<script type="text/javascript" src="/js/jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
function lookup(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {
    $('#inputString').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}
function fill2(thisValue) {
    $('#inputString2').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}

Used with the following HTML: 与以下HTML一起使用:

<tr><td><input type="text" size="50" name=line1 value="" id="inputString"
onkeyup="lookup(this.value);" onblur="fill();" /><div class="suggestionsBox"
id="suggestions" style="display: none;"> <div class="suggestionList"
id="autoSuggestionsList">

                &nbsp;
</div><div></td><td>1<input type="radio" name="rank1" value="1" 
<? if ($rank1=="1"){ echo "checked"; } ?> >2
<input type="radio" name="rank1" value="2" 
<? if ($rank1=="2"){ echo "checked"; } ?> >3
<input type="radio" name="rank1" value="3"
<? if ($rank1=="3"){ echo "checked"; } ?> > 
4<input type="radio" name="rank1" value="4" <? if ($rank1=="4"){ echo "checked"; } ?> >
<tr><td><input type="text" size="50" name=line1 value="" id="inputString2"
onkeyup="lookup(this.value);" onblur="fill2();" />
<div class="suggestionsBox" id="suggestions" style="display: none;">
            <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">
                &nbsp;
            </div>
        </div></td><td>
1<input type="radio" name="rank2" value="1" <? if ($rank2=="1"){ echo "checked"; } ?> >
2<input type="radio" name="rank2" value="2" <? if ($rank2=="2"){ echo "checked"; } ?> >
3<input type="radio" name="rank2" value="3" <? if ($rank2=="3"){ echo "checked"; } ?> >
4<input type="radio" name="rank2" value="4" <? if ($rank2=="4"){ echo "checked"; } ?> >

If you look at the JS at the top I presumed that by making two functions assigning data to two fields with different IDs would allow me to have autocomplete on each field (which works ok) but when I make a choice it always popuklates the first text box, regardless of which input box I start from...meaning if i start typing in box 1(id inputString), then make a choice from the autocomplete suggestions, box 1 gets filled. 如果您看一下顶部的JS,我认为通过使两个函数将数据分配给具有不同ID的两个字段,可以使我在每个字段上具有自动完成功能(可以正常使用),但是当我做出选择时,它总是弹出第一行文本框,无论我从哪个输入框开始...意味着如果我开始在框1(id inputString)中键入,然后从自动完成建议中进行选择,框1就会被填充。 But If I start typing in box 2(id inputString2) and get suggestions, click a suggestion, still box 1(id inputString) gets populated and not box 2(id inputString2). 但是,如果我开始在方框2(id inputString2)中输入并获得建议,请单击一个建议,仍然填充方框1(id inputString),而不是方框2(id inputString2)。

Any help would be appreciated. 任何帮助,将不胜感激。

Regards 问候

Darren 达伦

from http://api.jquery.com/id-selector/ : 来自http://api.jquery.com/id-selector/

Each id value must be used only once within a document. 每个id值在文档中只能使用一次。 If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. 如果为多个元素分配了相同的ID,则使用该ID的查询将仅选择DOM中的第一个匹配元素。 This behavior should not be relied on, however; 但是,不应依赖这种行为。 a document with more than one element using the same ID is invalid. 包含多个使用相同ID的元素的文档无效。

The reason why you are having your problem is because you have two ids with the same id: 您遇到问题的原因是因为您有两个具有相同ID的ID:

id=autoSuggestionsList

I suggest that you change them to autoSuggestionsList1 and autoSuggestionsList2 and then change your function: 我建议您将它们更改为autoSuggestionsList1autoSuggestionsList2 ,然后更改功能:

function lookup(inputString, selectorToUse) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#'+selectorToUse).html(data);
            }
        });
    }
} // lookup

and then change lookup(this.value) 然后更改lookup(this.value)

to lookup(this.value, "autoSuggestionsList1") and lookup(this.value, "autoSuggestionsList2") . lookup(this.value, "autoSuggestionsList1")lookup(this.value, "autoSuggestionsList2")

Hope that helps! 希望有帮助! Please mark as answer if it did. 如果有,请标记为答案。

i have made it work without the selectorToUse trick. 我没有选择器使技巧。 The problem comes from you selectors in your post function. 问题来自于帖子功能中的选择器。 Notice how i changed the lookup function (with "myInputString" class), and the selectors in the post function. 请注意我如何更改查找功能(使用“ myInputString”类)以及发布功能中的选择器。 The way you called lookup function, you didnt have access to $(this). 您调用查找函数的方式无法访问$(this)。

I have also revorked the html tags so jquery can navigate them (use of .parent() function) 我还撤销了html标签,以便jquery可以导航它们(使用.parent()函数)

php file: php文件:

<?php
  die($_POST['queryString']);
?>

Script: 脚本:

<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
    $(function(){
        $(".myInputString").keyup(function() { // previous lookup function now anonymous
            var inputString = $(this).attr("value");
            var curTag = $(this);
            if(inputString.length == 0) {
                // Hide the suggestion box.
                $('#suggestions').hide();
            } else {
                $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
                    if(data.length >0) {
                        curTag.parent().find('.suggestionsBox').show();
                        curTag.parent().find('.suggestionList').html(data);
                    }
                });
            }
        });
    });

function fill(thisValue) {
    $('#inputString').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}
function fill2(thisValue) {
    $('#inputString2').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}
</script>

and the form: 以及形式:

<table>
<tr>
<td>
        <input type="text" size="50" name=line1 value="" id="inputString" class="myInputString"  onblur="fill();" />
        <div class="suggestionsBox" id="suggestions" style="display: none;">
            <div class="suggestionList" id="autoSuggestionsList">
                A
            </div>
            </div>
                &nbsp;
<?php $rank1 = 1;  $rank2 = 2; ?>
</td>
</tr>
1<input type="radio" name="rank1" value="1"
<?php if ($rank1=="1"){ echo "checked"; } ?> >2
<input type="radio" name="rank1" value="2"
<?php if ($rank1=="2"){ echo "checked"; } ?> >3
<input type="radio" name="rank1" value="3"
<?php if ($rank1=="3"){ echo "checked"; } ?> >
4<input type="radio" name="rank1" value="4" <? if ($rank1=="4"){ echo "checked"; } ?> >
<tr><td><input type="text" size="50" name=line1 value="" id="inputString2" class="myInputString"
 onblur="fill2();" />
<div class="suggestionsBox" id="suggestions" style="display: none;">
            <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">B
                &nbsp;
            </div>
        </div></td>
        </tr>
        </table>
1<input type="radio" name="rank2" value="1" <? if ($rank2=="1"){ echo "checked"; } ?> >
2<input type="radio" name="rank2" value="2" <? if ($rank2=="2"){ echo "checked"; } ?> >
3<input type="radio" name="rank2" value="3" <? if ($rank2=="3"){ echo "checked"; } ?> >
4<input type="radio" name="rank2" value="4" <? if ($rank2=="4"){ echo "checked"; } ?> >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM