创建了一个新元素,在准备好文档后无法调用jquery方法 [英] new element created that calling to jquery method after ready document doesn't work

查看:75
本文介绍了创建了一个新元素,在准备好文档后无法调用jquery方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

$(document).ready(function() {

//  When change the value in select it will send id to controller in order to return the price of service.
$(".select-service").change(function(){
    var element_id = $(this).attr('id');
    $.post("/services/get_price_of", {
        message: this.value
    },
    function(data){
        $("#" + element_id).parent().find(".price-input").val(data);

    })
});

});

该页面加载了一个选择,但我可以动态添加更多选择.问题在于,对于所有动态添加的选择,早期事件均不起作用. 有人可以帮我吗?

The page is loaded with one select but I can add more select dynamically. The problem is that for all select added dynamically, the event early does not work. Can someone help me?

致谢.

P.D:对不起,我英语不好.

P.D: sorry for my poor english.

推荐答案

使用jquery on

http://api.jquery.com/on/

$(document).ready(function() {

    $("select").on("change", ".select-service", function(event){
         var element_id = $(this).attr('id');
        $.post("/services/get_price_of", {
            message: this.value
        },
        function(data){
            $("#" + element_id).parent().find(".price-input").val(data);

        })
    });
});

从jQuery 1.7开始,不推荐使用.live()方法

As of jQuery 1.7, the .live() method is deprecated

这篇关于创建了一个新元素,在准备好文档后无法调用jquery方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆