简体   繁体   English

JavaScript .Dialog动态按钮名称

[英]JavaScript .Dialog Dynamic button names

So I have something similar to this 所以我有类似的东西

   $("#id").html(msg2show).dialog({
        //Other attributes
        buttons: {
           "Yes": function() {//Code},
           "No": function() {//Code}
         }
    });

I am adding other language support to my site. 我正在为我的网站添加其他语言支持。 Is there a way to dynamically change "Yes" and "No" with out extra duplicate code? 有没有办法动态更改“是”和“否”没有额外的重复代码?

I have tried using the following and replacing the strings above with variable names, however it seems to interpret the variable names as strings rather than the value of the variable. 我已经尝试使用以下代码并用变量名替换上面的字符串,但它似乎将变量名称解释为字符串而不是变量的值。

var yes = 'Si';
var no = 'No';

Build your buttons object before passing it to .dialog() , so you can use your variables as keys with bracket notation: 在将按钮对象传递给.dialog()之前构建它们,这样您就可以将变量用作带括号表示法的键:

var yes = 'Si';
var no = 'No';
var buttons = {};
buttons[yes] = function() {};
buttons[no] = function() {};
$("#id").html(msg2show).dialog({
    //Other attributes
    buttons: buttons
});

use the text attribute to set the names of the button. 使用text属性设置按钮的名称。 Look at this Example 看看这个例子

   $( "#dialog" ).dialog({

   buttons: [
        {
         text: "OK",
         click: function() {
          $( this ).dialog( "close" );
     }
   }
 ]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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