Bootbox.js文档
- 我爱模板网
bootbox.js 4.x. 版本文档
使用Bootbox的注意事项,查阅Known Limitations了解更多。
简单的alert对话框,按ESC键或点击关闭按钮来关闭它。
This is the simplest usage of Bootbox, requiring only the text of the message you wish to show.
bootbox.alert("Your message here…")
Your message can also contain HTML.
bootbox.alert("Your message <b>here…</b>")
All Bootstrap modals, unlike native alerts, confirms, or prompts, generate non-blocking events. Because of this limitation, code that should not be evaluated until your user has dismissed your alert should be placed (or called) within a callback function:
bootbox.alert("Your message here…", function(){ /* your callback code */ })
Alerts can be customized, using the options described below.
bootbox.alert({
size: "small",
title: "Your Title",
message: "Your message here…",
callback: function(){ /* your callback code */ }
})
A confirm dialog with a cancel and a confirm button. Pressing the ESC key or clicking close dismisses the dialog and invokes the callback as if the user had clicked the cancel button.
Confirm dialogs require a callback function.
The simplest method of using the confirm()
dialog requires the text of the message you wish to show and a callback to handle the user's selection.
The callback function is passed a value of true or false, depending on which button the user pressed.
bootbox.confirm("Are you sure?", function(result){ /* your callback code */ })
All Bootstrap modals, unlike native alerts, confirms, or prompts, generate non-blocking events. Keep that in mind when using the confirm()
dialog, as it is not a drop-in replacement for native confirm dialogs. Any code that depends on the user's selection must be placed in the
callback function.
Confirm dialogs can be customized, using the options described below.
bootbox.confirm({
size: "small",
message: "Are you sure?",
callback: function(result){ /* result is a boolean; true = OK, false = Cancel*/ }
})
A dialog which prompts for user input. Pressing the ESC key or clicking close dismisses the dialog and invokes the callback as if the user had clicked the cancel button.
Prompt dialogs require a callback function.
The simplest usage of the prompt()
dialog requires the text of the message you wish to show and a callback to handle
the user's input. The value entered will be null if the user cancelled or dismissed
the dialog; otherwise it is passed the value of the text input.
bootbox.prompt("What is your name?", function(result){ /* your callback code */ })
All Bootstrap modals, unlike native alerts, confirms, or prompts, generate non-blocking events. Keep that in mind when using the prompt()
dialog, as it is not a drop-in replacement for native prompt dialogs. Any code that depends on the user's input must be placed in the
callback function.
Prompt dialogs can also be customized, using the options described below.
bootbox.prompt({
size: "small",
title: "What is your name?",
callback: function(result){ /* result = String containing user input if OK clicked or null if Cancel clicked */ }
})
Prompt requires the title
option (when using the options object) and disallows the message
option.
Prompt dialogs are (by default) single-line text inputs. You can modify the type of prompt Bootbox generates using the prompt-only options below.
Name | Description |
---|---|
value |
You can set the initial value of the prompt using the |
inputType |
Changes the type of input generated for the prompt dialog. Valid values for
|
inputOptions |
If you specify
|
Please see the 例s page for more 例s of prompt dialogs.
A completely customisable dialog method which takes a single argument - an options object.
Custom dialogs do not close automatically when the ESC key is pressed; you must
implement this behavior yourself using the onEscape
option.
The minimum required to create a custom dialog is the message
option, which will create a non-dismissable
dialog (useful as a "loading" overlay).
bootbox.dialog({ message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>' })
Please see the 例s page for more 例s of custom dialogs.
Bootbox dialogs can be configured using the options below.
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
message |
String | Element |
Text (or markup) displayed in the dialog. Required for alert, confirm, and custom dialogs |
||||||
title | String | Element |
Adds a header to the dialog and places this text (or markup) in an Required for prompts |
||||||
callback |
Function |
Required for confirm and prompt An alert callback should not supply an argument; it will be ignored if it does:
Confirm and prompt callbacks must supply an argument for the result; for confirm, it will be
a
or
Default: |
||||||
onEscape | Boolean | Function |
Allows the user to dismiss the dialog by hitting ESC, which will invoke this function.
Default: |
||||||
show | Boolean |
Whether the dialog should be shown immediately. Default: |
||||||
backdrop | Boolean |
Whether the dialog should be have a backdrop or not. Also determines whether clicking on the backdrop dismisses the modal.
Default:
* When this value is set to |
||||||
closeButton | Boolean |
Whether the dialog should have a close button or not. Default: |
||||||
animate | Boolean |
Animate the dialog in and out (requires a browser which supports CSS animations). Default: |
||||||
className | String |
An additional class to apply to the dialog wrapper. Default: |
||||||
size | String |
Adds the relevant Bootstrap modal size class to the dialog wrapper. Valid values are
Requires Bootstrap 3.1.0 or newer. Default: |
||||||
buttons |
Object |
Buttons are defined as JavaScript objects. The minimum shortform requirement to define a button is:
The complete definition of a button object is:
Each of the available button options can be overridden to use custom content (text or HTML) and CSS styles. For 例:
You cannot override the callbacks for the alert, confirm, and prompt dialog's buttons. |
The following functions can be called on an instance of a Bootbox object.
Allows the user to supply a function to be called when the dialog gets initialized.
var dialog = bootbox.dialog({
/* Your options... */
});
dialog.init(function(){
// Do something with the dialog...
});
init
can be called on any of the wrapper functions, as they all return a Bootbox object.
The following functions can be called statically.
This method allows the user to set many of the default options shown in the dialog 例. Many of these options are also applied to the basic wrapper methods and can be overridden whenever the wrapper methods are invoked with a single options argument:
Allows the user to select a locale rather than using setDefaults("locale", ...)
.
The locale settings are used to translate the three standard button labels: OK, CONFIRM, CANCEL
Default: en
Currently available:
bg_BG
br
cs
da
de
el
en
es
et
fa
fi
fr
he
hu
hr
id
it
ja
lt
lv
nl
no
pl
pt
ru
sq
sv
th
tr
zh_CN
zh_TW
Allows the user to add a custom translation for each of the built-in command buttons. The values
object should be in this format:
{
OK : '',
CANCEL : '',
CONFIRM : ''
}
Allows the user to remove a locale from the available locale settings.
Hide all currently active Bootbox dialogs. Individual dialogs can be closed as per normal Bootstrap dialogs:
dialog.modal('hide')
Using Bootbox does have some caveats, as noted below.
All Bootstrap modals, unlike native alerts, confirms, or prompts, generate non-blocking events. Because of this limitation, code that should not be evaluated until a user has dismissed your dialog should be placed (or called) within the callback function of the dialog.
This is a limitation of the Bootstrap modal plugin, as noted in the official Bootstrap documentation. While it is functionally possible to trigger multiple modals, custom CSS and/or JavaScript code is required for each layer of modal to display properly.