2010-10-25 23 views
8

Basado en el código siguiente para mostrar un diálogo JQuery, el texto del botón aparece como la literal "b" en comparación con el valor de la variable b.jQuery diálogo modal botón de texto

Ie: showWarningDialog('myBody', 'myTitle', 'go') muestra un cuadro de diálogo con un botón etiquetado b en lugar de go.

¿Cómo se puede obtener go para aparecer?

function showWarningDialog(theBody, theTitle, buttonText) { 
    var t = "Warning"; 
    if (theTitle != null) { 
     t = theTitle; 
    } 

    var b = "Ok"; 
    if (buttonText != null) { 
     b = buttonText; 
    } 

    $("#div-dialog-warning div").text(theBody); 

    $("#div-dialog-warning").dialog({ 
     title: t, 
     resizable: false, 
     height: 160, 
     modal: true, 
     buttons: { 
      b : function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 
} 

Respuesta

13

As per the jQuery UI docs, el nombre del botón proviene de la llave del botón en el objeto buttons. En cuyo caso, sustituir este bit:

$("#div-dialog-warning").dialog({ 
    title: t, 
    resizable: false, 
    height: 160, 
    modal: true, 
    buttons: { 
     b : function() { 
      $(this).dialog("close"); 
     } 
    } 
}); 

con esto:

var buttonOpts = {}; 
buttonOpts[b] = function() { 
    $(this).dialog("close"); 
}; 
$("#div-dialog-warning").dialog({ 
    title: t, 
    resizable: false, 
    height: 160, 
    modal: true, 
    buttons: buttonOpts 
}); 

Tienes que tratar b como una variable, por lo tanto, el uso de buttonOpts[b] en lugar de lo que hizo, que es el equivalente de usando buttonOpts.b.

0

{b: 'blah'} wil significa que se necesita b como nombre de variable. definir la matriz a mano podría solucionarlo, aunque no puedo imaginar que no haya una sintaxis especial para eso var buttons = {}; botones [b] = función() {}; $(). Diálogo ({botones: botones});

0

Creo que le falta el parámetro en la matriz de congig. No debería ser algo así como

buttons: { 
     b : function() { 
      $(this).dialog("close"); 
     }, 
     buttonText: b 
} 
+0

No hay opción 'buttonText'. Según los documentos, 'La clave de propiedad es el texto del botón'. –

2

esto es lo que hay que añadir faet inicializar ryou su diálogo:

$('div.ui-dialog-buttonpane button:contains(b)').empty().html(b); 

es probable que desee cambiar el nombre de B a ser algo un poco más descriptivo y único sin embargo.

Cuestiones relacionadas