简体   繁体   中英

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:

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. Look at this Example

   $( "#dialog" ).dialog({

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

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