带有表单 JS 的甜蜜提醒(3 个输入)

问题描述 投票:0回答:3

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

有一种方法可以自定义警报,文档说,但不允许从我的框架(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

javascript html forms css sweetalert
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' }
  })

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

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

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


0
投票

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

    $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);   
});
© www.soinside.com 2019 - 2024. All rights reserved.