问题
When opening a jQuery-UI dialog, how can I hide a button (for instance, hide the "Save" button)?
http://jsfiddle.net/ba6jwh54/1/
<!-- head -->
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js" type="text/javascript"></script>
<!-- body -->
<div id="dialog" class="dialog" title="My Title"></div>
<a href="#" id="open">open</a>
// javascript
$(document).ready(function() {
$('#open').click(function() {
$("#dialog").dialog("open");
});
$("#dialog").dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
open: function() {
var dialog = $(this);
console.log('dialog', dialog);
var buttons = dialog.dialog("option", "buttons");
console.log('buttons', buttons);
//Change names this way...
buttons[0].text = 'Save2';
buttons[1].text = 'Cancel2';
dialog.dialog("option", "buttons", buttons);
//How do I hide a button (i.e. hide Save button)?
},
buttons: [{
text: 'SAVE',
click: function() {
alert('save');
$(this).dialog("close");
}
}, {
text: 'CANCEL',
click: function() {
$(this).dialog("close");
}
}]
});
});
回答1:
The easiest* way is to get a hold of the current dialog's widget element and .find()
the button inside it:
open: function () {
var $widget = $(this).dialog("widget");
$widget.find(".ui-dialog-buttonpane button:first").hide();
}
Updated Fiddle
Easier than finding all the button
elements on the page and guessing which one is which.
回答2:
I just added an id to the button, and updated the click function to hide it.
$(document).ready(function () {
$('#open').click(function () {
$("#dialog").dialog("open");
$("#save").hide();
});
$("#dialog").dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
open: function () {
var dialog = $(this);
console.log('dialog', dialog);
var buttons = dialog.dialog("option", "buttons");
console.log('buttons', buttons);
//Change names this way...
buttons[0].text = 'Save2';
buttons[1].text = 'Cancel2';
dialog.dialog("option", "buttons", buttons)
//How do I hide a button (i.e. hide Save button)?
},
buttons: [{
text: 'SAVE',
id: "save",
click: function () {
alert('save');
$(this).dialog("close");
}
}, {
text: 'CANCEL',
click: function () {
$(this).dialog("close");
}
}]
});
});
http://jsfiddle.net/ba6jwh54/2/
来源:https://stackoverflow.com/questions/26533692/how-to-hide-dialog-buttons-when-a-jquery-ui-dialog-is-opened