我正在使用jQuery UI对话框进行ajax表单提交。当用户点击保存按钮并返回保存时,我想将我的保存按钮的文本更改为等待。请帮助我。
虽然这个问题非常老,但我发现答案非常简单,而且这里没有给出。
$().button()
函数来修改按钮。 JavaScript代码如下:
$('#dialog').dialog({
buttons:[{
id: 'buttonId',
click: function() {
// your function
}]
});
$('#buttonId').button('option', 'label', 'Please wait...');
假设你正在使用带有buttons选项的.dialog(),您可以将自定义类分配给提交按钮,然后通过分配给span (ui-button-text)的类来针对内部按钮文本:
$("#dialog-test").dialog({
title: "My dialog",
buttons:
[
{
text: "Submit",
click: function() {
$(".my-custom-button-class > .ui-button-text").text("Please Wait...");
$("#some-form").submit();
},
'class': 'my-custom-button-class'
}
]
});
当您使用submit()提交数据并完成保存时,您可以使用同一调用来将文本设置回您想要的内容。
//-> Change to Loading Notice:
setButton('.settingsDialog', 'Save', 'Saving...', false);
setTimeout(function() { setButton('.settingsDialog', 'Saving...', 'Save', true); }, 800 );},
//Select the Dialog Buttons
function getDialogButton( dialog_selector, button_name ) {
var buttons = $( dialog_selector + ' .ui-dialog-buttonpane button' );
for ( var i = 0; i < buttons.length; ++i ) {
var jButton = $( buttons[i] );
if ( jButton.text() == button_name )
{
return jButton;
}
}
return null;
}
//Button Controller for AJAX Loading:
function setButton(sDialogClass, sButtonName, sNewButtonName, bEnabled){
var btn = getDialogButton(sDialogClass, sButtonName);
btn.find('.ui-button-text').html(sNewButtonName);
if (bEnabled) {
btn.removeAttr('disabled', 'true');
btn.removeClass( 'ui-state-disabled' );
} else {
btn.attr('disabled', 'true');
btn.addClass( 'ui-state-disabled' );
}
}
$(".ui-dialog-buttonpane button:contains('Save') span").text("Please wait...");
请注意正确的方法来动态创建多个按钮:
'buttons', [
{
text: "Download",
click: function () {...}
},
{
text: "Not now",
click: function () {...}
}
]
$("#dialog-test").dialog({
title: "My dialog",
buttons:
[
{
text: "Submit",
click: function() {
$(".my-custom-button-class > .ui-button-text").text("Please Wait...");
//You may use $(this) as selector or allso $("#dialog-test")
$(this).parent().children('.ui-dialog-buttonpane').children().children().html('Please wait..');
//As you have only one button, it should not matter to specify child element, but you can like this:
//$($(this).parent().children('.ui-dialog-buttonpane').children().children()[0]).html('Please wait..');
$("#some-form").submit();
},
'class': 'my-custom-button-class'
}
]
});
$('#setActionButton').button('option').text('new text');
在进行 AJAX 调用之前使用 $().text('Please Wait')
,然后在成功回调函数中的最后一个操作添加 $().text('Save')
。
请注意,为此,您必须使用 button
元素,而不是 input
元素:
<button>Text here</button>
对于那些不想添加额外的ID / Class或不想重复单击回调函数的人:
// initialization
$('#my-dialog').dialog({
buttons: [
{
text: 'Submit',
click: function () {...}
}
]
});
// You can simply change button text by this way
$('#my-dialog').dialog('option', 'buttons.0.text', 'wait...');