来自变量的jQuery UI对话框按钮

时间:2023-01-23 19:46:53

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

我有变量在jquery ui对话框中保存按钮的已翻译标签。

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?

有没有办法让它与变量数组键一起使用?

3 个解决方案

#1


38  

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


1  

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

#3


0  

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.

我知道这已经有4年了,但这是我遇到的一个问题的最佳结果。这是我的劳动成果。

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

#1


38  

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


1  

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

#3


0  

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.

我知道这已经有4年了,但这是我遇到的一个问题的最佳结果。这是我的劳动成果。

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