0

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"> &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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
saurav
  • 21
  • 1
  • 3

2 Answers2

0

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

retuor
  • 11
  • 3
0

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.

david
  • 4,218
  • 3
  • 23
  • 25