2012-03-26 116 views
0

我有一個選擇列表菜單包含產品,並在此菜單上我使用onChangeevent。當您選擇任何產品,它加載product.Below的公司名稱是我使用的功能,jQuery觸發onKeyup事件

function getcompany(element) {   

     var strURL="getcompany.php?product="+element.value; 
     var req = getXMLHTTP(); 

     if (req) { 

      req.onreadystatechange = function() { 
       if (req.readyState == 4) { 
        // only if "OK" 
        if (req.status == 200) { 

         element.parentNode.parentNode.getElementsByClassName('companydiv')[0].innerHTML=req.responseText;      
        } else { 
         alert("There was a problem while using XMLHTTP:\n" + req.statusText); 
        } 
       }    
      }   
      req.open("GET", strURL, true); 
      req.send(null); 
     }  
    } 

而我的HTML代碼,

<select name="product" onChange="getcompany(this)"> 
    <option value="1" >Product1</option> 
    <option value="2" >Product2</option> 
    </select> 
    <div class="companydiv">Product result will be shown here</div> 

上面的代碼工作,但現在我想使用jquery自動完成而不是選擇列表菜單因爲我的產品列表包含超過1000個產品。

我的自動完成代碼,但我不知道我錯了

<input name="product" id="product" type="text" onkeyup="getcompany(this)" /> 

我已經查了一些其他的答案,但我不太滿意的人,他們中的一些下面

jQuery AutoComplete Trigger Change Event

Jquery Autocomplete onChange event

http://forum.jquery.com/topic/autocomplete-and-change-event

+0

什麼問題?函數不被調用?嘗試在自動填充功能中使用「選擇」。 – Hadas 2012-03-26 12:25:44

+0

@哈達斯是的函數不會被調用,我也嘗試按鍵和其他事件,但他們都沒有工作 – Arif 2012-03-26 12:28:58

回答

1

我在這裏找不到你的錯誤,但是我可以做你正在做的事情。我在這裏使用jquery。 jsFiddle

+0

當你完成打字並離開文本框時,你的函數被調用。但在我的情況下,我只是輸入** a **和包含顯示給我的所有產品,然後我點擊我想要的一個 – Arif 2012-03-26 12:55:26

+2

**您可能會喜歡「.keyup()」** [link] http:// jsfiddle達網絡/ agentmilindu/QtC2q/7 / – 2012-03-26 12:58:24