将变量传递给jquery模态,怎么样?

时间:2011-05-30 08:23:01

标签: javascript jquery

这是我的JQuery UI模式窗口的代码:

$('<p>Message</p>').dialog({
    modal: true,
    buttons: {
      Ok: function() {
         $( this ).dialog( "close" );
      }
    }
});

如何传递变量而不是常量字符串(参见上面的Ok

2 个答案:

答案 0 :(得分:1)

来自jQuery UI dialog button text as a variable

var button_name = 'Test';    
var dialog_buttons = {};

dialog_buttons[button_name] = function(){ 
    closeInstanceForm(Function); 
}    
dialog_buttons['Cancel'] = function(){ 
   $(this).dialog('close'); 
}

$('#instanceDialog').dialog({ 
    buttons: dialog_buttons 
});

答案 1 :(得分:1)

试试这个:

var dynamicButtons= {};
var buttonOne = 'Press Me Please!';
var buttonTwo = 'No way';
dynamicButtons[buttonOne] = function() {
    // do your stuffs
};
dynamicButtons[buttonTwo] = function() {
   // do your stuffs
};
$('<p>Message</p>').dialog({
    modal: true,
    buttons: dynamicButtons
});

演示:http://jsbin.com/omawu4/2/