简体   繁体   中英

JQuery UI Dialog - Ajax Update on success $(this).dialog('close');

Having issues referencing $(this) from within a the nested ajax 'success' function... I know this is a scope issue, but can't seem to find a clean way to close the dialog on a successful update. Thanks for any help.

$("#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('');
        }
    });

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

You need to have a copy of that variable, like this:

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');
   }
});

Since this is in a different context on return, you need to capture it and pass it into the closure :)

Try it with $.proxy()

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

You can 'pass' the scope from 'above' to a function with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM