【问题标题】:JQuery UI Dialog - Ajax Update on success $(this).dialog('close');JQuery UI 对话框 - Ajax 更新成功 $(this).dialog('close');
【发布时间】:2010-04-06 14:47:30
【问题描述】:

在嵌套的 ajax 'success' 函数中引用 $(this) 时遇到问题...我知道这是一个范围问题,但似乎找不到在成功更新时关闭对话框的干净方法。感谢您的帮助。

$("#dialog_support_option_form").dialog({
        width: 400,
        height: 180,
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
            'Save Support Option': function(){
                $.ajax({
                    type: 'POST',
                    url: "support_options/create_support_option.php",
                    data: $(this).find('form').serialize(),
                    success: function(data){
                        $("#list_support_options").html(data);
                        $(this).dialog('close');
                    }
                });
            },
            'Cancel': function(){
                $(this).dialog('close');
            }
        },
        close: function(){
            $(this).find('input').val('');
        }
    });

【问题讨论】:

    标签: jquery user-interface jquery-ui-dialog


    【解决方案1】:

    您应该使用 ajax 选项 context: $(this), 将回调范围设置为选定元素。

    【讨论】:

    • 轰隆隆,砰砰砰。正是我想要的。听说过有关此选项的谣言,但找不到。谢谢。
    • 太棒了,你也帮了我
    【解决方案2】:

    您需要拥有该变量的副本,如下所示:

    var dlg = $(this);
    $.ajax({
       type: 'POST',
       url: "support_options/create_support_option.php",
       data: $(this).find('form').serialize(),
       success: function(data){
         $("#list_support_options").html(data);
         dlg.dialog('close');
       }
    });
    

    由于this 在返回时处于不同的上下文中,因此您需要捕获它并将其传递给闭包:)

    【讨论】:

    • @Jonathan Julian - 所有这些都是闭包内的变量,你认为context: 没有设置一些吗? :)
    • 是的,这行得通,但我希望不要使用额外的变量
    【解决方案3】:

    试试$.proxy()

    success: $.proxy(function(data){
       $(this).dialog('close');
    }, this);
    

    您可以将范围从“上方”“传递”到使用它的函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多