请问如何在jQuery UI对话框中,使用变量作为按钮文本?我想要创建一个动态的按钮名称。
请问如何在jQuery UI对话框中,使用变量作为按钮文本?我想要创建一个动态的按钮名称。
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 });
你可以给对话框中的按钮分配一个ID,然后使用标准的jQuery进行操作。
$("#dialog_box").dialog({
autoOpen: false,
modal: true,
resizable: false,
buttons: [{
text: "Ok",
"id": "btnOk",
click: function () {
//okCallback();
},
}, {
text: "Cancel",
click: function () {
//cancelCallback();
},
}],
close: function () {
//do something
}
});
设置按钮文本:
var newLabel = "Updated Label";
$("#btnOk").html('<span class="ui-button-text">'+ newLabel +'</span>')
$("#btnOk").html('<span class="ui-button-text">'+'新标签'+'</span>');
- Alfonso Rubalcava$("#dialog_box").dialog({
buttons: {
'ButtonA': function() {
//... configure the button's function
}
});
$('.ui-dialog-buttonpane button:contains(ButtonA)').attr("id","dialog_box_send-button");
$('#dialog_box_send-button').html('Send')
$(function() {
// using textbox value instead of variable
$("#dialog").dialog({
width: 400,
buttons: [
{ text: $("#buttonText0").val(), click: function() { $(this).dialog("close"); } },
{ text: $("#buttonText1").val(), click: function() { $(this).dialog("close"); } }
]
});
$("#updateButtonText").on("click", function() {
var $buttons = $("#dialog").dialog("widget").find(".ui-dialog-buttonpane button");
console.log($buttons.get());
$buttons.eq(0).button("option", "label", $("#buttonText0").val());
$buttons.eq(1).button("option", "label", $("#buttonText1").val());
// few more things that you can do with button widget
$buttons.eq(0).button("option", "icons", { primary: "ui-icon-check" });
$buttons.eq(1).button("disable");
$("#dialog").dialog("open");
});
});
@import url("https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.min.css");
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<div id="dialog" title="Sample Dialog">
<p>Proceed?</p>
</div>
<p style="text-align: center;">
<input type="text" id="buttonText0" value="OK">
<input type="text" id="buttonText1" value="Cancel">
<input type="button" id="updateButtonText" value="Update Button Text">
</p>
buttons: [
{ text: $("#buttonText0").val() },
{ text: $("#buttonText1").val() }
]
在使用 $(selector).val()
时,您可以在使用“text”选项时放置一个字符串变量,如果尝试以下操作,则无法这样做:confirmStr: function() {
$(this).dialog("close");
},
- Mayer M $("#dialog_box").dialog({
buttons: {
[
text:"OK",
click: function() {
//... configure the button's function
}
]
});
$("#dialog_box").dialog("option", "buttons", { "Close": function() { $(this).dialog("close"); } });
buttons
是一个 button
对象数组。你的开头 [
应该与之前的 {
交换位置,关闭方式同样如此。 - nothingisnecessary
var btnArray = [
{ text: "Add Entry",
click: function(){
this.retVal = true;
addRowIntoTemplateManifest();
$(this).dialog('close');
}
},
{ text: "Cancel",
click: function(){
this.retVal = false;
$(this).dialog('close');
}
}
];
一个自定义函数displayConfirmDialog_Dynamic()被编写,它接受对话框标题、对话框文本和按钮数组。调用此函数的方式如下:displayConfirmDialog_Dynamic("Add Template Manifest Entry?", "Do you want to add following Cuboid Entry to Template Manifest?\nCuboidNane: '" + json.cuboidName + "' CuboidId: " + json.cuboidId + "\non Server:"
+ json.serverUrl , btnArray );
//Confirmation dialog Dynamic Buttons
function displayConfirmDialog_Dynamic(dlgTitle, message, btnArray)
{
var retVal;
$("div#dialog-confirm").find("p").html(message);
var confirmDlg = $( "#dialog-confirm" ).dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
title: dlgTitle,
buttons: btnArray,
show:{effect:'scale', duration: 700},
hide:{effect:'explode', duration: 700}
});
confirmDlg.dialog('option', 'buttons', btnArray);
confirmDlg.prev(".ui-dialog-titlebar").css({"background":"#ffc000", "color":"#ffffff", "font-size":"13px", "font-weight":"normal"}) ;
confirmDlg.dialog("open");
}
title
和<p>
标签的内容。<div id="dialog-confirm" title="Empty the recycle bin?" style="display:none;">
<p>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
而且不要忘记
$($("button", $(".info_dialog").parent())[1]).html("<span class='ui-button-text'>Button text here.</span>");
这将起作用
$($("button", $("#dialogId").parent())[NUMBER_OF_YOUR_BUTTON]).text("我的文本");
使用内联行为完全可以实现:
function ConfirmDialog() {
var yesButtonName = "Yes";
var noButtonName = "No";
this.showMessage = function(message, callback, argument) {
var $dialog = $('<div></div>')
.html(message)
.dialog({
modal: true,
closeOnEscape: true,
buttons: [
{
text:yesButtonName,
click: function() {
if (callback && typeof(callback) === "function") {
if (argument == 'undefined') {
callback();
} else {
callback(argument);
}
} else {
$(this).dialog("close");
}
}
},
{
text:noButtonName,
click: function() {
$(this).dialog("close");
}
}
]
});
$dialog.dialog("open");
};
this.setYesButtonName = function(name) {
yesButtonName = name;
return this;
};
this.setNoButtonName = function(name) {
noButtonName = name;
return this;
};
}
创建 ConfirmDialog 类的对象。
this.CONFIRM_DIALOG = new ConfirmDialog();
在任何事件上调用方法,比如 onclick()。
OK_DIALOG.setYesButtonName('想结婚').showMessage('最糟糕的主意!!');
任务完成了!
$("span.ui-button-text:contains('OLD BUTTON NAME')").html('NEW BUTTON NAME');