2013-02-26 31 views
0

我真正的新jquery.Well我有兩個輸入文本字段,我已經成功地通過了以下功能如何使用jquery populate輸入文本字段?

function lookup(inputString,tableName,colName,divId,listId) { 
     if(inputString.length == 0) { 
      // Hide the suggestion box. 
      $(divId).hide(); 
     } else { 
      $.post("customers.php", {queryString: ""+inputString+"",table:""+tableName+"",col:""+colName+""}, function(data){ 
       if(data.length >0) { 
        $(divId).show(); 
        $(listId).html(data); 
       } 
      }); 
     } 
    } // lookup 

到attatch同時在輸入字段列表自我暗示和我的形式看起來像這樣

<div> 
      <form> 
       <div> 
        Location 
        <br /> 
        <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value,'countries','value','#suggestions','#autoSuggestionsList');" onblur="fillValues(this.value,'#inputString','#suggestions');" autocomplete="off"/> 
       </div> 

       <div class="suggestionsBox" id="suggestions" style="display: none;"> 
        <img src="http://localhost/ci/css/images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" /> 
        <div class="suggestionList" id="autoSuggestionsList" > 
         &nbsp; 
        </div> 
       </div> 

       <div> 
        Favourite Product 
        <br /> 
        <input type="text" size="30" value="" id="inputString1" onkeyup="lookup(this.value,'product','value','#suggestions1','#autoSuggestionsList1');" 
"fillValues(this.value,'#inputString1','#suggestions1');" autocomplete="off"/> 
       </div> 

       <div class="suggestionsBox" id="suggestions1" style="display: none;"> 
        <img src="http://localhost/ci/css/images/upArrow.png" style="position: relative; top: -12px;left: 30px;" alt="upArrow" /> 
        <div class="suggestionList" id="autoSuggestionsList1" > 
         &nbsp; 
        </div> 
       </div> 
      </form> 
     </div> 

和fillValues功能看起來像這樣

function fillValues(thisVal,inputId,divId) 
    { 
     $(inputId).val(thisValue); 
     setTimeout("$("+divId+").hide();",200) 

    } 

現在,當用戶點擊一個值列表中輸入文本上字段應該填充該值,但我無法這樣做。我錯過了什麼?

p.s. autosuggestion列表已成功顯示,我的customers.php工作正常。

+0

通過jquery使用它的id來定位一個元素,它就像這個$(「#elementID」) – fatiDev 2013-02-26 11:41:21

+0

你也說,當用戶點擊一個值時,你的代碼在哪裏?你寫的函數將不會被調用,直到你捕獲用戶點擊 – fatiDev 2013-02-26 11:47:20

+0

http://api.jquery.com/click/ – fatiDev 2013-02-26 11:49:32

回答

0

我想你的函數必須是這樣的,奧尤必須添加一個「#」爲目標的元素,其ID

function fillValues(thisVal,inputId,divId) 
    { 
     $("#"+inputId).val(thisValue); 
     setTimeout("$(#"+divId+").hide();",200) 

    } 

的目標,其id元素,我們使用「#」 目標元素我們的班級使用'。'

+0

嘿,我已經通過#號的值,所以我不需要再次放置哈希值:) – user1234 2013-02-27 03:06:54

相關問題