开发者

Auto suggest PHP ajax with two input boxes not working with the given sample code

开发者 https://www.devze.com 2023-04-12 15:34 出处:网络
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"></scr

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 ==开发者_StackOverflow中文版 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>

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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消