【发布时间】:2011-04-01 19:50:22
【问题描述】:
我敢肯定这很简单,但让我发疯......
我有以下自动完成脚本:
<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);
}
与以下 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">
</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">
</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"; } ?> >
如果您查看顶部的 JS,我推测通过创建两个函数将数据分配给具有不同 ID 的两个字段将允许我对每个字段进行自动完成(这可以正常工作)但是当我做出选择时它总是 popuklates第一个文本框,无论我从哪个输入框开始...意思是如果我开始在框 1(id inputString)中输入,然后从自动完成建议中做出选择,框 1 将被填充。但是,如果我开始在框 2(id inputString2) 中输入并获得建议,单击建议,框 1(id inputString) 仍然会被填充,而不是框 2(id inputString2)。
任何帮助将不胜感激。
问候
达伦
【问题讨论】:
-
你能把它发布到 jFiddle 吗?调试起来会更容易...
-
没关系,我想通了。请参阅下面的答案。
标签: php jquery autocomplete