我有一个代码来获取搜索建议,下面是代码,
<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 {
$('#country').addClass('load');
$.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').fadeIn();
$('#suggestionsList').html(data);
$('#country').removeClass('load');
}
});
}
}
function fill(thisValue) {
$('#country').val(thisValue);
setTimeout("$('#suggestions').fadeOut();", 600);
}
</script>
<form id="form" action="#">
<div id="suggest">To: <br />
<input type="text" size="40" value="" id="country" onkeyup="suggest(this.value);" onblur="fill();" class="" />
<div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="arrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="suggestionsList"> </div>
</div>
</div>
</form></td><td>
<form id="torm" action="#">
<div id="suggest">From: <br />
<input type="text" size="40" value="" id="country" onkeyup="suggest(this.value);" onblur="fill();" class="" />
<div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="arrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="suggestionsList"> </div>
</div>
</div>
</form>
只提供了一个表格并且代码工作正常,但是当我添加另一个表单,然后我在那里输入一个键时,它仍然会获取第一个表单的建议,请帮助我
答案 0 :(得分:0)
这可能是2个具有相同ID的建议框的问题..为2个表单提供单独的ID并将其传递给suggest()函数
答案 1 :(得分:0)
因为你的ID对于“country”,“suggestions”和“suggestionsList”是相同的,如果结果是那些ID,它将获得第一个ID,你应该将ID作为唯一ID。