jQuery UI Dialog Buttons from variables

萝らか妹 提交于 2019-11-27 13:38:39

问题


I have variables holding the translated labels for buttons inside a jquery ui dialog.

I cannot fill the button array key with the variable itself, and can't find any way to let it treat my variable just as string.

translations['ok'] = 'ok';
translatinos['cancel'] = 'cancel';

// not working
jQuery('#foo').dialog({
    buttons:
    {
        translations['ok']: function() { alert('foo-ok'); },
        translations['cancel']: function() { alert('foo-cancel'); }
    }
});

// working
jQuery('#bar').dialog({
    buttons:
    {
        "Ok": function() { alert('bar-ok'); },
        "Cancel": function() { alert('bar-cancel'); }
    }
});

Is there any way to get this to work with variable array keys?


回答1:


You can try this, may be it helps:

var buttonsOpts = {}
buttonsOpts[translations["ok"]] = function ....
buttonsOpts[translations["cancel"]] = function ....
jQuery('#bar').dialog({
   buttons : buttonsOpts
});

Hope it helps!




回答2:


jQuery('#bar').dialog({
   buttons : [
       {
        text: translations.ok,
        click: function(){}
       },
       {
        text: translations.cancel,
        click: function(){}
       },
   ]
});



回答3:


I know this is 4 years old, but it is the top result for an issue I have been having. Here was the results of my labor.

Simply call the function in a mouse or keyboard event, reference a function (without parentheses), define the buttons or set blank, set a title, and set the text to be displayed.

function confirmDialogue(fn, value, ok, cancel, title, text){
    if (typeof ok == "undefined" || ok == ""){ok = "Ok";}
    if (typeof cancel == "undefined" || cancel == ""){cancel = "Cancel";}
    var buttonsOpts = {};
    buttonsOpts[ok] = function() {fn(value);$( this ).dialog( "destroy" );}
    buttonsOpts[cancel] = function() {$( this ).dialog( "destroy" );}

    var NewDialog = $('<div id="dialogConfirm"><p>' + text + '</p></div>');
    NewDialog.dialog({
        title: title,
        dialogClass: "dialogue",
        modal: true,
        height: "auto",
        width: "auto",
        show: true,
        hide: true,
        close: function(){$(this).dialog('destroy');},
        buttons: buttonsOpts
    });
    return false;
}


来源:https://stackoverflow.com/questions/1357281/jquery-ui-dialog-buttons-from-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!