Sweet Alert with form JS (3 input)

Sweet Alert with form JS (3 input)

我想在 sweet alert 中实现一个表单。我可以在里面放一个输入,标题和 body.

有一种方法可以自定义警报,文档说了,但是不允许从我的框架 (customizecss.com) 或我的 [=27] 加载 class css3 =].

我正在尝试在警报中包含一个输入,这样:

swal({
       title: "HTML <small>Title</small>!",
       text: "<input type='text'><label class='my-label'>Name</label>",
       html: true 
});

它不起作用,只显示标签...为什么?

不知道有什么方法可以做到...

谢谢!

甜蜜警报 -> https://github.com/t4t5/sweetalert

您似乎可以使用 'input' 类型的 Sweet Alert(而不是 'error' 或 'warning')。

    $window.swal({title: 'Text Entry', text: 'Put some text here, please.', type: 'input'});

来自 Github 自述文件: 记录用户输入的提示模式:

sweetAlert({
  title: "An input!",
  text: 'Write something interesting:',
  type: 'input',
  showCancelButton: true,
  closeOnConfirm: false,
  animation: "slide-from-top"
  }, function(inputValue){
    console.log("You wrote", inputValue);   
});

我知道这已经过时了,但我会为所有网络搜索者回答这个问题: 你需要给一个数组作为参数,使用 html 属性:

swal({
html: "<form action='verify.php' method='post'>
    <input id='user' type='email'>
    <input id='pass' type='password'>
    <input id='idno' type='number'>
    <submit>
</form>"});

verify.php 替换为您需要数据的任何页面,并将 html: 之后的字符串替换为您需要的任何 HTML 代码。

唯一的缺点是人们需要点击表单上的提交按钮,而不是 SweetAlert 自己的按钮,尽管也有办法删除 SweetAlert 自己的按钮。

您可以使用这个插件来实现您想要的:

https://github.com/taromero/swal-forms

它以语法友好的方式在模态内创建表单:

 swal.withForm({
    title: 'More complex Swal-Forms example',
    text: 'This has different types of inputs',
    showCancelButton: true,
    confirmButtonColor: '#DD6B55',
    confirmButtonText: 'Get form data!',
    closeOnConfirm: true,
    formFields: [
      { id: 'name', placeholder: 'Name Field' },
      { id: 'nickname', placeholder: 'Add a cool nickname' },
      { id: 'password', type: 'password' },

      { name: 'sex', value: 'Male', type: 'radio' },
      { name: 'sex', value: 'Female', type: 'radio' },

      { name: 'skills', value: 'JS', type: 'checkbox' },
      { name: 'skills', value: 'Ruby', type: 'checkbox' },
      { name: 'skills', value: 'Java', type: 'checkbox' },

      { id: 'select',
        type: 'select',
        options: [
          {value: 'test1', text: 'test1'},
          {value: 'test2', text: 'test2'},
          {value: 'test3', text: 'test3'},
          {value: 'test4', text: 'test4'},
          {value: 'test5', text: 'test5'}
        ]}
    ]
  }, function (isConfirm) {
    // do whatever you want with the form data
    console.log(this.swalForm) // { name: 'user name', nickname: 'what the user sends' }
  })