自动建议带有两个输入框的 PHP ajax 不适用于给定的示例代码

发布于 2024-12-09 12:32:50 字数 1980 浏览 0 评论 0原文

我有一个代码来获取搜索建议,下面给出的是代码:

<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"> &nbsp; </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"> &nbsp; </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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂归处 2024-12-16 12:32:50

这可能是 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

伤痕我心 2024-12-16 12:32:50

因为“国家/地区”、“建议”和“建议列表”的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文