在open方法中更改jQueryUI对话框按钮文本

时间:2012-11-29 13:24:41

标签: jquery jquery-ui jquery-ui-dialog

打开jQueryUI对话框后,我想执行GET请求,并根据响应更改按钮文本。经过几个小时的挣扎,我终于得到了以下工作。这真的是最好/唯一吗?感谢

$("#dialog").dialog({
    open        : function() {
        var dialog=$(this);
        $.get('ajax.php', function (data) {
            var buttons=dialog.dialog( "option", "buttons" );
            buttons[1].text=(data==1)?"CANCEL":"CLOSE";
            var buttons=dialog.dialog( "option", "buttons" ,buttons);
        });
    },
    buttons     : [
        {
            text    : 'SAVE',
            click    : function() {}
        },
        {
            text    : 'CANCEL',
            click    : function() {}
        }
    ]    
});

1 个答案:

答案 0 :(得分:2)

您可以像这样更改按钮文字......

以下给出的是没有ajax

function setbutton(button1, button2) {
var btns = {};
btns[button1] = function() {
    //your function
   $( this ).dialog( "close" );
};
btns[button2] = function() {
    // Do nothing
    //your function
    $( this ).dialog( "close" );
};

document.getElementById('dialogshow').innerHTML = "<div>open with given button text</div>";


$( "#dialogshow" ).dialog({
    autoOpen: true,
    width: 450,
    height: 200,
    modal: true,
    position: 'center',
    modal: true,
    buttons: btns
});
}
$('.test').click(function() {
setbutton('start', 'End');//in here button name you want..
});

See the Live Demo