【问题标题】:select onfocusin do something is not working选择 onfocusin 做某事不起作用
【发布时间】:2014-11-03 03:19:59
【问题描述】:

我正在尝试使用 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
});

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

【问题讨论】:

    标签: jquery select delegates focusin


    【解决方案1】:

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

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

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

    【讨论】:

    • 它似乎运作良好。但是如果我想让它在没有 focusin 事件的情况下自动加载呢?
    • @ChandlerLee 我不确定你的意思...?
    • 我的意思是,如果我使用 focusin 事件,那么我需要在 ajax 加载之前单击选择并附加选择。有没有办法像这样使用委托加载 ajax 内容?
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    相关资源
    最近更新 更多