2014-11-03 91 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