0

如果 jquery select 在 ajax 成功函数之外,我无法选择通过 ajax 调用插入的任何元素。如果我成功地移动所有东西,它就可以正常工作。

任何有经验的人都可以帮助我了解正在发生的事情以及如何处理这个问题?我正在搜索 2 天已经找不到任何东西:S

这有效:

//Show levels using ajax
//define variables
var levels = 2; //level count can be rewritten to come from DB and not be hardcoded like now
var completed_calls = 0; //count tasks start from 0
var views = new Array();
//All level display ajax function
function create_views(level) {
        $.ajax({
            url: "actions.php",
            type: "GET",
            data: "show_level=" + level,
            cache: false,
            success: function (data){
                views[level] = '<li data-level="' + level + '">' + data + '</li>';

                //Count completed tasks
                completed_calls++;

                //if last level is being called append and activate slider
                if (levels == completed_calls) {
                    //append views to container
                    $(".slides_container").append(views.join(' '));
                    //activate slider
                    var unislider = $(".slides_container").dpUniSlider({
                        //loop: false,
                        draggable: false
                    });

                    //Login and Registration windows on login slide, one closes the other opens up
                    //+ login registration links in header for logged out users
                        //Login
                        $("#header-login, #logwindow").on("click", function(){
                                $("#regsitrationform").hide();
                                $("#register h2").hide();
                            $("#loginform").show().css("opacity",0);
                            unislider.goToSlide(2);
                            $("#loginform, #register h2").delay(480).fadeTo(0, 1);
                                $("#logwindow").removeClass("active").addClass("notactive");
                                $("#regwindow").removeClass("notactive").addClass("active");
                            return false;
                        });

                        //Registration
                        $("#header-register, #regwindow").on("click", function(){
                                $("#loginform").hide();
                                $("#register h2").hide();
                            $("#regsitrationform").show().css("opacity",0);
                            unislider.goToSlide(2);
                            $("#regsitrationform, #register h2").delay(480).fadeTo(0, 1);
                                $("#regwindow").removeClass("active").addClass("notactive");
                                $("#logwindow").removeClass("notactive").addClass("active");
                            return false;
                        });

                        //Pop-up logo tooltip: close
                            $(".close").on("click", function(){
                                $(".logo").fadeTo("slow", 1);
                                $(this).parent().fadeOut("fast");
                                return false;
                            });

                        //Pop-up logo tooltip: clear
                            $(".clear").on("click", function(){
                                $(this).parent().find("input").val("");
                                return false;
                            });

                        //Activate Georgian keyboard for some logos
                            $("input[data-lang=ge]").GeoKBD();
                }
            }
        });
}
//Loop through all levels and display views
for (var i=1; i<=levels; i++) {create_views(i);}

这不会:

//Show levels using ajax
//define variables
var levels = 2; //level count can be rewritten to come from DB and not be hardcoded like now
var completed_calls = 0; //count tasks start from 0
var views = new Array();
//All level display ajax function
function create_views(level) {
        $.ajax({
            url: "actions.php",
            type: "GET",
            data: "show_level=" + level,
            cache: false,
            success: function (data){
                views[level] = '<li data-level="' + level + '">' + data + '</li>';

                //Count completed tasks
                completed_calls++;

                //if last level is being called append and activate slider
                if (levels == completed_calls) {
                    //append views to container
                    $(".slides_container").append(views.join(' '));
                    //activate slider
                    var unislider = $(".slides_container").dpUniSlider({
                        //loop: false,
                        draggable: false
                    });
                }
            }
        });
}
//Loop through all levels and display views
for (var i=1; i<=levels; i++) {create_views(i);}





//Ajax call to display-update single level
function update_view(level) {

}





                    //Login and Registration windows on login slide, one closes the other opens up
                    //+ login registration links in header for logged out users
                        //Login
                        $("#header-login, #logwindow").on("click", function(){
                                $("#regsitrationform").hide();
                                $("#register h2").hide();
                            $("#loginform").show().css("opacity",0);
                            unislider.goToSlide(2);
                            $("#loginform, #register h2").delay(480).fadeTo(0, 1);
                                $("#logwindow").removeClass("active").addClass("notactive");
                                $("#regwindow").removeClass("notactive").addClass("active");
                            return false;
                        });

                        //Registration
                        $("#header-register, #regwindow").on("click", function(){
                                $("#loginform").hide();
                                $("#register h2").hide();
                            $("#regsitrationform").show().css("opacity",0);
                            unislider.goToSlide(2);
                            $("#regsitrationform, #register h2").delay(480).fadeTo(0, 1);
                                $("#regwindow").removeClass("active").addClass("notactive");
                                $("#logwindow").removeClass("notactive").addClass("active");
                            return false;
                        });

                        //Pop-up logo tooltip: close
                            $(".close").on("click", function(){
                                $(".logo").fadeTo("slow", 1);
                                $(this).parent().fadeOut("fast");
                                return false;
                            });

                        //Pop-up logo tooltip: clear
                            $(".clear").on("click", function(){
                                $(this).parent().find("input").val("");
                                return false;
                            });

                        //Activate Georgian keyboard for some logos
                            $("input[data-lang=ge]").GeoKBD();
4

1 回答 1

0

如果 jquery select 在 ajax 成功函数之外,我无法选择通过 ajax 调用插入的任何元素。如果我成功地移动所有东西,它就可以正常工作。

听起来问题是您正在尝试将事件处理程序绑定到尚不存在的元素。尝试在执行代码时出现的父元素上使用on,delegatelive(取决于您使用的 jQuery 版本)。

就像是:

$(".slides_container").on('click', 'li[data-level]', function () {...});
于 2013-03-26T05:57:14.580 回答