Jquery.confirm 메세지창 플러그인
-
- 첨부파일 : jquery_confirm_plugin.jpg (106.3K) - 다운로드
-
808회 연결
-
0회 연결
본문
Getting started
https://github.com/craftpip/jquery-confirm/wiki/Migrate-from-v2-to-v3
Installation
Download
Download the Latest version and use files in the /dist/
folder.
via CDN:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>
via Bower:
Simply copy these links
$ bower install craftpip/jquery-confirm
via Npm:
$ npm install jquery-confirm
Dependencies:
- Bootstrap by Twitter >= v3 (optional, if you want to use responsive layouts)
- jQuery library > v1.8
useBootstrap: false
Quick Usage
$.alert
adds one button (okay) if no buttons are specified, this lets the user to close the modal.
example alert$.alert({
title: 'Alert!',
content: 'Simple alert!',
});
$.confirm
if no buttons are specified, two buttons (Okay & cancel) will be added.
$.confirm({
title: 'Confirm!',
content: 'Simple confirm!',
buttons: {
confirm: function () {
$.alert('Confirmed!');
},
cancel: function () {
$.alert('Canceled!');
},
somethingElse: {
text: 'Something else',
btnClass: 'btn-blue',
keys: ['enter', 'shift'],
action: function(){
$.alert('Something else?');
}
}
}
});
Showing prompt using confirm
Simply add form to the content and bind the events you want.
This form can also be loaded via ajax.
$.confirm({
title: 'Prompt!',
content: '' +
'<form action="" class="formName">' +
'<div class="form-group">' +
'<label>Enter something here</label>' +
'<input type="text" placeholder="Your name" class="name form-control" required />' +
'</div>' +
'</form>',
buttons: {
formSubmit: {
text: 'Submit',
btnClass: 'btn-blue',
action: function () {
var name = this.$content.find('.name').val();
if(!name){
$.alert('provide a valid name');
return false;
}
$.alert('Your name is ' + name);
}
},
cancel: function () {
//close
},
},
onContentReady: function () {
// bind to events
var jc = this;
this.$content.find('form').on('submit', function (e) {
// if the user submits the form by pressing enter in the field.
e.preventDefault();
jc.$$formSubmit.trigger('click'); // reference the button and click it
});
}
});
$.dialog
removes buttons and explicitly shows the closeIcon (×)
example dialog$.dialog({
title: 'Text content!',
content: 'Simple modal!',
});
$.fn.confirm
This can be used to bind to a element directly
If no buttons are defined, the default buttons (okay and cancel) will be added, and default action for okay will be to redirect on the given href. use this.$target to get the clicked element.
<a class="twitter" data-title="Goto twitter?" href="http://twitter.com/craftpip">Goto twitter</a>
$('a.twitter').confirm({
content: "...",
});
$('a.twitter').confirm({
buttons: {
hey: function(){
location.href = this.$target.attr('href');
}
}
});
Shorthand usage
The shorthand thingy takes in two string arguments, first one is the content of the dialog and second the title of the dialog. The second argument is optional.
Lazy open
If you want to create a instance of jconfirm and save it for later use.
var a = $.confirm({
lazyOpen: true,
});
a.open();
a.close();
a.toggle(); // toggle open close.
NOTE : The $.confirm()
, $.dialog()
& $.alert()
methods are alias of jconfirm()
.
All three methods indirectly call the jconfirm base function altering the provided options.
ADVANCED: this.$body
is the body div for jquery-confirm. You can find and alter any element at run time.
Buttons
Its all what you need to know about buttons in jquery-confirm v3.
Defining multiple buttons is introduced in v3.
this.$$heyThere
for heyThere button in the below snippet. this is to change the button properties at run time.
Definition
Buttons can be defined in the following fashion, pretty self explanatory.
$.confirm({
buttons: {
hello: function(helloButton){
// shorthand method to define a button
// the button key will be used as button name
},
hey: function(heyButton){
// access the button using jquery
this.$$hello.trigger('click'); // click the 'hello' button
this.$$hey.prop('disabled', true); // disable the current button using jquery method
// jconfirm button methods, all methods listed here
this.buttons.hello.setText('Helloooo'); // setText for 'hello' button
this.buttons.hey.disable(); // disable with button function provided by jconfirm
this.buttons.hey.enable(); // enable with button function provided by jconfirm
// the button's instance is passed as the first argument, for quick access
heyButton === this.buttons.hey
},
heyThere: {
text: 'Hey there!', // text for button
btnClass: 'btn-blue', // class for the button
keys: ['enter', 'a'], // keyboard event for button
isHidden: false, // initially not hidden
isDisabled: false, // initially not disabled
action: function(heyThereButton){
// longhand method to define a button
// provides more features
}
},
}
});
Button text
$.confirm({
buttons: {
hey: function () {
// here the button key 'hey' will be used as the text.
$.alert('You clicked on "hey".');
},
heyThere: {
text: 'hey there!', // With spaces and symbols
action: function () {
$.alert('You clicked on "heyThere"');
}
}
}
});
Button style
Simply apply classes to buttons to style them.
Jconfirm comes bundled with btn-blue
btn-green
btn-red
btn-orange
btn-purple
btn-default
btn-dark
These classes only work inside jquery-confirm's modal
Other bootstrap options are btn-primary
btn-inverse
btn-warning
btn-info
btn-danger
btn-success
.
$.confirm({
buttons: {
info: {
btnClass: 'btn-blue',
action: function(){}
},
danger: {
btnClass: 'btn-red any-other-class', // multiple classes.
...
},
warning: {
btnClass: 'btn-warning',
...
},
}
});
Button keys
A 'key' feature of jquery-confirm! (pun intended)
Listen to keyup events for individual buttons. A button can listen for multiple keys at a time.
If multiple modals are stacked together, only the topmost modal will listen for keyboard event.
a to Z, tilde (the ` key), enter, shift, tab, capslock, ctrl, win, alt, esc, space.
$.confirm({
content: 'Time to use your keyboard, press shift, alert, A or B',
buttons: {
specialKey: {
text: 'On behalf of shift',
keys: ['shift', 'alt'],
action: function(){
$.alert('Shift or Alt was pressed');
}
},
alphabet: {
text: 'A, B',
keys: ['a', 'b'],
action: function(){
$.alert('A or B was pressed');
}
}
}
});
In this example, the YES button is hidden, The user can only use the keyboard to click the button!
$.confirm({
title: false,
content: 'Imagine a very critical action here! <br> ' +
'Please press <strong style="font-size: 20px;">Y</strong> to proceed.',
buttons: {
yes: {
isHidden: true, // hide the button
keys: ['y'],
action: function () {
$.alert('Critical action <strong>was performed</strong>.');
}
},
no: {
keys: ['N'],
action: function () {
$.alert('You clicked No.');
}
},
}
});
Button functions
jquery-confirm provides a set of functions for a nice and clean way to alter your buttons in run-time.
if this is not enough you can use this.$$<buttonName>
to get the jquery DOM element
A full list of functions for buttons.
Function | Code | Description |
---|---|---|
setText | this.buttons.<buttonName>.setText(text) | The text you want to set. |
addClass | this.buttons.<buttonName>.addClass(className) | Add a class to the button |
removeClass | this.buttons.<buttonName>.removeClass(className) | remove a class to the button |
disable | this.buttons.<buttonName>.disable() | Disable the button |
enable | this.buttons.<buttonName>.enable() | Enable the button |
show | this.buttons.<buttonName>.show() | Show the button via CSS |
hide | this.buttons.<buttonName>.hide() | Hide the button via CSS |
$.confirm({
closeIcon: true, // explicitly show the close icon
buttons: {
buttonA: {
text: 'button a',
action: function (buttonA) {
this.buttons.resetButton.setText('reset button!!!');
this.buttons.resetButton.disable();
this.buttons.resetButton.enable();
this.buttons.resetButton.hide();
this.buttons.resetButton.show();
this.buttons.resetButton.addClass('btn-red');
this.buttons.resetButton.removeClass('btn-red');
// or
this.$$resetButton // button's jquery element reference, go crazy
this.buttons.buttonA == buttonA // both are the same.
return false; // prevent the modal from closing
}
},
resetButton: function (resetButton) {
}
}
});
Customizing
Dialog type
Dialog types helps give the user a hint as to what the dialog is about
Error/Red dialog Success/Green dialog Non animated type dialog orange blue purple dark$.confirm({
title: 'Encountered an error!',
content: 'Something went downhill, this may be serious',
type: 'red',
typeAnimated: true,
buttons: {
tryAgain: {
text: 'Try again',
btnClass: 'btn-red',
action: function(){
}
},
close: function () {
}
}
});
Icons
Give meaning to your dialog with custom icons.
Read about Font Awesome here .
$.confirm({
icon: 'glyphicon glyphicon-heart',
title: 'glyphicon'
});
$.confirm({
icon: 'fa fa-warning',
title: 'font-awesome'
});
$.confirm({
icon: 'fa fa-spinner fa-spin',
title: 'Working!',
content: 'Sit back, we are processing your request!'
});
Close icon
jQuery confirm uses ×
html entity for this close symbol, however you can use Any icon of your choice (fa, glyphicon, zmdi)
closeIcon
is set to null
. That means, if buttons are not defined the closeIcon will be shown, else will not be shown. To explicitly show
closeIcon
set it to a truthy value and vise versa. Turn on closeIcon explicitly
댓글목록 0