6

我想在甜蜜警报中实现一个表单。我可以在里面只放一个输入,带有标题和正文。

有一种方法可以自定义警报,文档说,但不允许从我的框架(customizecss.com)或我的 style.css 加载类 css3。

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

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

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

我想知道是否有什么方法可以做到...

谢谢!

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

4

3 回答 3

4

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

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' }
  })
于 2017-09-01T10:20:12.683 回答
2

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>"});

用你需要数据的任何页面替换,然后用你需要的任何 HTML 代码verify.php替换字符串。html:

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

于 2017-03-17T15:18:59.127 回答
0

看起来您可以使用带有“输入”类型的甜蜜警报(而不是“错误”或“警告”)。

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

来自 Github README:记录用户输入的提示模式:

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);   
});
于 2015-06-04T23:12:20.127 回答