• js-jquery-SweetAlert2【三】INPUT TYPES


    1、text

    swal({
      title: 'Input something',
      input: 'text',
      showCancelButton: true,
      inputValidator: function (value) {
        return new Promise(function (resolve, reject) {
          if (value) {
            resolve()
          } else {
            reject('You need to write something!')
          }
        })
      }
    }).then(function (result) {
      swal({
        type: 'success',
        html: 'You entered: ' + result
      })
    })
    View Code

    2、email

    swal({
      title: 'Input email address',
      input: 'email'
    }).then(function (email) {
      swal({
        type: 'success',
        html: 'Entered email: ' + email
      })
    })
    View Code

    3、password

    swal({
      title: 'Enter your password',
      input: 'password',
      inputAttributes: {
        'maxlength': 10,
        'autocapitalize': 'off',
        'autocorrect': 'off'
      }
    }).then(function (password) {
      if (password) {
        swal({
          type: 'success',
          html: 'Entered password: ' + password
        })
      }
    })
    View Code

    4、textarea

    swal({
      input: 'textarea',
      showCancelButton: true
    }).then(function (text) {
      if (text) {
        swal(text)
      }
    })
    View Code

    5、select

    swal({
      title: 'Select Ukraine',
      input: 'select',
      inputOptions: {
        'SRB': 'Serbia',
        'UKR': 'Ukraine',
        'HRV': 'Croatia'
      },
      inputPlaceholder: 'Select country',
      showCancelButton: true,
      inputValidator: function (value) {
        return new Promise(function (resolve, reject) {
          if (value === 'UKR') {
            resolve()
          } else {
            reject('You need to select Ukraine :)')
          }
        })
      }
    }).then(function (result) {
      swal({
        type: 'success',
        html: 'You selected: ' + result
      })
    })
    View Code

    6、radio

    // inputOptions can be an object or Promise
    var inputOptions = new Promise(function (resolve) {
      setTimeout(function () {
        resolve({
          '#ff0000': 'Red',
          '#00ff00': 'Green',
          '#0000ff': 'Blue'
        })
      }, 2000)
    })
    
    swal({
      title: 'Select color',
      input: 'radio',
      inputOptions: inputOptions,
      inputValidator: function (result) {
        return new Promise(function (resolve, reject) {
          if (result) {
            resolve()
          } else {
            reject('You need to select something!')
          }
        })
      }
    }).then(function (result) {
      swal({
        type: 'success',
        html: 'You selected: ' + result
      })
    })
    View Code

    7、checkbox

    swal({
      title: 'Terms and conditions',
      input: 'checkbox',
      inputValue: 1,
      inputPlaceholder:
        'I agree with the terms and conditions',
      confirmButtonText:
        'Continue <i class="fa fa-arrow-right></i>',
      inputValidator: function (result) {
        return new Promise(function (resolve, reject) {
          if (result) {
            resolve()
          } else {
            reject('You need to agree with T&C')
          }
        })
      }
    }).then(function (result) {
      swal({
        type: 'success',
        text: 'You agreed with T&C :)'
      })
    })
    View Code

    8、file

    swal({
      title: 'Select image',
      input: 'file',
      inputAttributes: {
        accept: 'image/*'
      }
    }).then(function (file) {
      var reader = new FileReader
      reader.onload = function (e) {
        swal({
          imageUrl: e.target.result
        })
      }
      reader.readAsDataURL(file)
    })
    View Code

    9、range

    swal({
      title: 'How old are you?',
      type: 'question',
      input: 'range',
      inputAttributes: {
        min: 8,
        max: 120,
        step: 1
      },
      inputValue: 25
    })
    View Code

    10、多输入框

    不支持多输入框,但是可以使用html and preConfirm自己来实现

    swal({
      title: 'Multiple inputs',
      html:
        '<input id="swal-input1" class="swal2-input">' +
        '<input id="swal-input2" class="swal2-input">',
      preConfirm: function () {
        return new Promise(function (resolve) {
          resolve([
            $('#swal-input1').val(),
            $('#swal-input2').val()
          ])
        })
      },
      onOpen: function () {
        $('#swal-input1').focus()
      }
    }).then(function (result) {
      swal(JSON.stringify(result))
    }).catch(swal.noop)
    View Code

  • 相关阅读:
    将帅问题
    堆栈(链栈)
    堆栈(基础实现原理 顺序栈)
    双向链表
    冒泡排序 (泛型版)
    maven
    jboss数据源配置
    仓库介绍,nexus的安装
    mave聚合继承
    mac mysql 安装
  • 原文地址:https://www.cnblogs.com/bjlhx/p/6757336.html
Copyright © 2020-2023  润新知