2014-11-03 23 views
0

我想使用AJAX加載一些輸入&選擇顯示後來在頁面上。我正在做這樣的事情。選擇onfocusin做什麼不工作

<input type='product' alt='2'/> 

$("input[type='product']").on("focusin", function(){ 
    var product_type_ids = $(this).attr('alt'); 
    $(this).autocomplete({ 
     // ajax load the information here 
    }); 
}); 

<select type='product' alt='2'/> 

$("select[type='product']").on("focusin", function(){ 
    var product_type_ids = $(this).attr('alt'); 
    // some codes here 
}); 

的結果是選擇和投入,從頁面呈現的開始存在,但不是元素後出現了確定。有人知道這裏發生了什麼嗎?謝謝!

回答

1

這是事件代表團。改變你的選擇器。

$("body").on("focusin", "input[type='product']", function(){ 

$("body").on("focusin", "select[type='product']", function(){ 

這將允許元素的事件傳播到靜態父級,如本例中的「body」。這對於所有未來的元素來說也是一樣。

+0

它似乎運作良好。但是如果我想讓它在沒有focusin事件的情況下自動加載呢? – 2014-11-03 03:47:23

+0

@ChandlerLee我不確定你的意思是...? – Ohgodwhy 2014-11-03 04:02:56

+0

我的意思是如果我使用focusin事件,那麼我需要在ajax加載之前單擊select並追加選擇。有沒有辦法像這樣使用委託加載ajax內容? – 2014-11-03 04:12:50