自动建议带有两个输入框的 PHP ajax 不适用于给定的示例代码
我有一个代码来获取搜索建议,下面给出的是代码:
<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>
仅给出了一个表单并且代码工作正常,但是当我添加另一个表单并在那里输入一个密钥时,它仍然会获取第一个表单的建议形式。请帮我。
I have got a code to get search suggestion, given below is the 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 {
$('#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>
Only one form was given and the code was working fine, but when I added another form, and I enter a key there, it still fetches the suggestion for the first form. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是 2 个建议框具有相同 id 的问题。为 2 个表单提供单独的 id 并将其传递给 suggest() 函数
This might be a problem with the 2 suggestion boxes having the same id .. give separate ids for the 2 forms and pass it to the suggest() function
因为“国家/地区”、“建议”和“建议列表”的 ID 是相同的,如果结果转到这些 ID,它将找到找到的第一个 ID,因此您的 ID 应该是唯一的。
because you your ID's are the same for "country", "suggestions" and "suggestionsList", if the results are going to those ID it will got the tihe first ID's found, you should have IDs as being unique.