0

我正在尝试使用 AJAX 加载稍后显示在页面上的一些输入和选择。我正在做这样的事情。

<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
});

对于从页面呈现开始就存在的选择和输入,结果是可以的,但对于稍后出现的元素则不行。有谁知道这里发生了什么?谢谢!

4

1 回答 1

1

这是事件委托。更改您的选择器。

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

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

这将允许元素的事件传播到静态父级,例如本例中的“body”。这也适用于所有未来的元素。

于 2014-11-03T03:32:43.243 回答