Question

When page load first time the page contents which are I load on Jquery.ready javascripts work perfect then when I use ajax to do some things and try to load page again javascript are not working on that second page which I try to load. I tried to solve this problem by using live() / on() methods but it didn't work.

$(document).ready(function () {
    $("#grupyonetimarea").load("/Panel/MusteriSourcePages/mus_grup_add.aspx");
});

$('#btnekleme').live('click', function () {
    $.ajax({
        type: "POST",
        url: "/Panel/MusteriSource/mus_kisiadd.aspx/mus_kisi_kaydet",
        data: "{ad:'" + $.trim($("#txtalt_mus_adi").val()) + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        before: $("#btnekleme").hide(),
        success: function (msg) {
            if (msg.d == "ok") {
                $("#grupyonetimarea").load("/Panel/MusteriSourcePages/mus_grup_add.aspx");
            } else $("#loaded").html(msg.d).show(500);
        },
        error: function (x, e) {
            alert("The call to the server side failed. " + x.responseText);
        }
    });
}
Was it helpful?

Solution

live is deprecated so use on() delegated event.. delegate it to closest static parent.. ( this might not be the issue but its safe to use on() for future if incase you want to update your jquery in near future...)..here is the link if you want to read more about on()

 $(document).on('click','#btnekleme', function () {.. //using closest static parent element is preferred here than the document

and you can send data as object in ajax

var inputValue= $.trim($("#txtalt_mus_adi").val());
data: {ad:inputValue},

OTHER TIPS

Thank u very much for all answers. i did it in that way i just made a function which is kontrolet() when i run that dunction it works good way which I want.

      $(document).on('click', '#btnekleme', function () {
                    $("#grupyonetimarea").load("/Panel/MusteriSourcePages/mus_grup_add.aspx");
                });

 function kontrolet() {
         $.ajax({
                            type: "POST",
                            url: "/Panel/MusteriSource/mus_kisiadd.aspx/mus_kisi_kaydet",
                            data: "{ad:'" + $.trim($("#txtalt_mus_adi").val()) + "'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            async: true,
                            cache: false,
                            before: $("#btnekleme").hide(),
                            success: function (msg) {
                                if (msg.d == "ok") {
                                    $("#grupyonetimarea").load("/Panel/MusteriSourcePages/mus_grup_add.aspx");
                                    alert("Kayıt işlemi başarılıdır.!");
                                    TextArealariClearET();
                                    $("#btnekleme").show();
                                }
                                else
                                    $("#loaded").html(msg.d).show(500);
                            },
                            error: function (x, e) {
                                alert("The call to the server side failed. " + x.responseText);
                            }
                        });
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top