【问题标题】:Jquery toggle fails on the data retrieved from Ajax Post从 Ajax Post 检索到的数据上的 Jquery 切换失败
【发布时间】:2016-12-27 14:54:33
【问题描述】:

我正在尝试使用 jQuery 插件来实现展开所有 Divs/折叠所有 Divs/展开或折叠单个 Divs,这是 Jquery 插件:http://www.adipalaz.com/experiments/jquery/multiple_expand_all_collapse_all.html。唯一的区别是我试图将这些效果应用于从 AJAX 帖子中检索到的 Div。

以下是我目前的流程:

从index.php 将数据发布到url(getData.php) 并检索Html DIVs-> 在Divs 上执行切换。 te DIV 所需的 CSS 已在 Index.PHP 上加载。

使用 jquery 和 CSS 将切换按钮附加到索引页面上检索到的 DIV。切换 div 所需的 java 脚本已经存在于 index.php 页面上。但是切换按钮不会出现在检索到的 div 上,因为它们甚至在检索内容之前就被触发了。我尝试使用 ajaxStop 来触发 ajax 完成,然后触发切换功能,但没有用。下面是我的示例代码。

     $.ajax({
                                type: 'POST',
                                url: 'getTicketSummary.php', 
                                data: {ref_no: ref_no},
                                success: function(data) {
                                    $('#wrapper').hide().html(data).slideDown('slow');
                                    $(function() {
     $("#content h3.expand").toggler();
        $("#content div.demo").expandAll({trigger: "h3.expand", ref: "h3.expand"});
    });
                            }
                        });
                    }
                });

$(function() {
     $("#content h3.expand").toggler();
        $("#content div.demo").expandAll({trigger: "h3.expand", ref: "h3.expand"});
    });

//这是我的切换功能,因为我使用的是 jquery 插件,expand.js,现在我尝试在 Post 请求后添加该功能,但它似乎不起作用。

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:

    下面的代码就足够了

     $.ajax({
           type: 'POST',
           url: 'getTicketSummary.php', 
           data: {ref_no: ref_no},
           success: function(data) {
                    $('#wrapper').hide().html(data).slideDown('slow',function(){
                      $("#content h3.expand").toggler();
                      $("#content div.demo").expandAll({trigger: "h3.expand", ref: "h3.expand"});
                    });
           }
      });
    

    【讨论】:

    • @chamika.. 非常感谢兄弟这就像魅力一样,无需使用额外的语句触发。
    【解决方案2】:

    没关系,

    我想通了。我使用 $('#wrapper').ajaxStop 来触发 ajax 请求的完成,并触发了一个函数以在完成时启用切换。

    我的代码是

        $.ajax({
                                    type: 'POST',
                                    url: 'getTicketSummary.php', 
                                    data: {ref_no: ref_no},
                                    success: function(data) {
                                        $('#wrapper').hide().html(data).slideDown('slow');
                                        $(function() {
         $("#content h3.expand").toggler();
            $("#content div.demo").expandAll({trigger: "h3.expand", ref: "h3.expand"});
        });
                                }
                            });
                        }
                    });
    
    $('#wrapper').ajaxStop(function() { 
         $("#content h3.expand").toggler();
            $("#content div.demo").expandAll({trigger: "h3.expand", ref: "h3.expand"});
        });
    

    希望对遇到类似问题的人有所帮助,谢谢!

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多