• 쇼핑몰
  • 커뮤니티
  • 북마크

Jquery

Jquery.confirm 메세지창 플러그인

익명
2020.05.01 13:39 5,491 0
  • - 첨부파일 : jquery_confirm_plugin.jpg (106.3K) - 다운로드

본문

Getting started

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
to use jconfirm without bootstrap set useBootstrap: false

Quick Usage

$.alert

adds one button (okay) if no buttons are specified, this lets the user to close the modal.

example alert
  1. $.alert({
  2. title: 'Alert!',
  3. content: 'Simple alert!',
  4. });

$.confirm

if no buttons are specified, two buttons (Okay & cancel) will be added.

example confirm
  1. $.confirm({
  2. title: 'Confirm!',
  3. content: 'Simple confirm!',
  4. buttons: {
  5. confirm: function () {
  6. $.alert('Confirmed!');
  7. },
  8. cancel: function () {
  9. $.alert('Canceled!');
  10. },
  11. somethingElse: {
  12. text: 'Something else',
  13. btnClass: 'btn-blue',
  14. keys: ['enter', 'shift'],
  15. action: function(){
  16. $.alert('Something else?');
  17. }
  18. }
  19. }
  20. });
Showing prompt using confirm

Simply add form to the content and bind the events you want.
This form can also be loaded via ajax.

example prompt
  1. $.confirm({
  2. title: 'Prompt!',
  3. content: '' +
  4. '<form action="" class="formName">' +
  5. '<div class="form-group">' +
  6. '<label>Enter something here</label>' +
  7. '<input type="text" placeholder="Your name" class="name form-control" required />' +
  8. '</div>' +
  9. '</form>',
  10. buttons: {
  11. formSubmit: {
  12. text: 'Submit',
  13. btnClass: 'btn-blue',
  14. action: function () {
  15. var name = this.$content.find('.name').val();
  16. if(!name){
  17. $.alert('provide a valid name');
  18. return false;
  19. }
  20. $.alert('Your name is ' + name);
  21. }
  22. },
  23. cancel: function () {
  24. //close
  25. },
  26. },
  27. onContentReady: function () {
  28. // bind to events
  29. var jc = this;
  30. this.$content.find('form').on('submit', function (e) {
  31. // if the user submits the form by pressing enter in the field.
  32. e.preventDefault();
  33. jc.$$formSubmit.trigger('click'); // reference the button and click it
  34. });
  35. }
  36. });

$.dialog

removes buttons and explicitly shows the closeIcon (×)

example dialog
  1. $.dialog({
  2. title: 'Text content!',
  3. content: 'Simple modal!',
  4. });

$.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.

Goto twitter

HTML
  1. <a class="twitter" data-title="Goto twitter?" href="http://twitter.com/craftpip">Goto twitter</a>
Javascript
  1. $('a.twitter').confirm({
  2. content: "...",
  3. });
  4. $('a.twitter').confirm({
  5. buttons: {
  6. hey: function(){
  7. location.href = this.$target.attr('href');
  8. }
  9. }
  10. });

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.

  1. $.alert('Content here', 'Title here'); try me
  2. $.confirm('A message', 'Title is optional'); try me
  3. $.dialog('Just to let you know'); try me

Lazy open

If you want to create a instance of jconfirm and save it for later use.

  1. var a = $.confirm({
  2. lazyOpen: true,
  3. });
  4. a.open();
  5. a.close();
  6. 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.

You can access the button element via 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.

  1. $.confirm({
  2. buttons: {
  3. hello: function(helloButton){
  4. // shorthand method to define a button
  5. // the button key will be used as button name
  6. },
  7. hey: function(heyButton){
  8. // access the button using jquery
  9. this.$$hello.trigger('click'); // click the 'hello' button
  10. this.$$hey.prop('disabled', true); // disable the current button using jquery method
  11. // jconfirm button methods, all methods listed here
  12. this.buttons.hello.setText('Helloooo'); // setText for 'hello' button
  13. this.buttons.hey.disable(); // disable with button function provided by jconfirm
  14. this.buttons.hey.enable(); // enable with button function provided by jconfirm
  15. // the button's instance is passed as the first argument, for quick access
  16. heyButton === this.buttons.hey
  17. },
  18. heyThere: {
  19. text: 'Hey there!', // text for button
  20. btnClass: 'btn-blue', // class for the button
  21. keys: ['enter', 'a'], // keyboard event for button
  22. isHidden: false, // initially not hidden
  23. isDisabled: false, // initially not disabled
  24. action: function(heyThereButton){
  25. // longhand method to define a button
  26. // provides more features
  27. }
  28. },
  29. }
  30. });

Button text

Button text test
  1. $.confirm({
  2. buttons: {
  3. hey: function () {
  4. // here the button key 'hey' will be used as the text.
  5. $.alert('You clicked on "hey".');
  6. },
  7. heyThere: {
  8. text: 'hey there!', // With spaces and symbols
  9. action: function () {
  10. $.alert('You clicked on "heyThere"');
  11. }
  12. }
  13. }
  14. });

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.

Using buttons that come with jconfirm! Using bootstrap buttons!
  1. $.confirm({
  2. buttons: {
  3. info: {
  4. btnClass: 'btn-blue',
  5. action: function(){}
  6. },
  7. danger: {
  8. btnClass: 'btn-red any-other-class', // multiple classes.
  9. ...
  10. },
  11. warning: {
  12. btnClass: 'btn-warning',
  13. ...
  14. },
  15. }
  16. });

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.

Available key options are:
a to Z, tilde (the ` key), enter, shift, tab, capslock, ctrl, win, alt, esc, space.
Click me!
  1. $.confirm({
  2. content: 'Time to use your keyboard, press shift, alert, A or B',
  3. buttons: {
  4. specialKey: {
  5. text: 'On behalf of shift',
  6. keys: ['shift', 'alt'],
  7. action: function(){
  8. $.alert('Shift or Alt was pressed');
  9. }
  10. },
  11. alphabet: {
  12. text: 'A, B',
  13. keys: ['a', 'b'],
  14. action: function(){
  15. $.alert('A or B was pressed');
  16. }
  17. }
  18. }
  19. });
Another example

In this example, the YES button is hidden, The user can only use the keyboard to click the button!

  1. $.confirm({
  2. title: false,
  3. content: 'Imagine a very critical action here! <br> ' +
  4. 'Please press <strong style="font-size: 20px;">Y</strong> to proceed.',
  5. buttons: {
  6. yes: {
  7. isHidden: true, // hide the button
  8. keys: ['y'],
  9. action: function () {
  10. $.alert('Critical action <strong>was performed</strong>.');
  11. }
  12. },
  13. no: {
  14. keys: ['N'],
  15. action: function () {
  16. $.alert('You clicked No.');
  17. }
  18. },
  19. }
  20. });

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
  1. $.confirm({
  2. closeIcon: true, // explicitly show the close icon
  3. buttons: {
  4. buttonA: {
  5. text: 'button a',
  6. action: function (buttonA) {
  7. this.buttons.resetButton.setText('reset button!!!');
  8. this.buttons.resetButton.disable();
  9. this.buttons.resetButton.enable();
  10. this.buttons.resetButton.hide();
  11. this.buttons.resetButton.show();
  12. this.buttons.resetButton.addClass('btn-red');
  13. this.buttons.resetButton.removeClass('btn-red');
  14. // or
  15. this.$$resetButton // button's jquery element reference, go crazy
  16. this.buttons.buttonA == buttonA // both are the same.
  17. return false; // prevent the modal from closing
  18. }
  19. },
  20. resetButton: function (resetButton) {
  21. }
  22. }
  23. });

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
  1. $.confirm({
  2. title: 'Encountered an error!',
  3. content: 'Something went downhill, this may be serious',
  4. type: 'red',
  5. typeAnimated: true,
  6. buttons: {
  7. tryAgain: {
  8. text: 'Try again',
  9. btnClass: 'btn-red',
  10. action: function(){
  11. }
  12. },
  13. close: function () {
  14. }
  15. }
  16. });

Icons

Give meaning to your dialog with custom icons.
Read about Font Awesome here .

Using font-awesome Animated font-awesome
  1. $.confirm({
  2. icon: 'glyphicon glyphicon-heart',
  3. title: 'glyphicon'
  4. });
  5. $.confirm({
  6. icon: 'fa fa-warning',
  7. title: 'font-awesome'
  8. });
  9. $.confirm({
  10. icon: 'fa fa-spinner fa-spin',
  11. title: 'Working!',
  12. content: 'Sit back, we are processing your request!'
  13. });

Close icon

jQuery confirm uses &times; html entity for this close symbol, however you can use Any icon of your choice (fa, glyphicon, zmdi)

By default 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

등록된 댓글이 없습니다.