【问题标题】:Sweetalert2 doesn't return true/falseSweetalert2 不返回真/假
【发布时间】:2016-12-03 14:11:57
【问题描述】:

我正在尝试使用 SweetAlert2。当我不想通过 $_POST 发送一些数据时,它可以工作。但是当我想通过 $_POST 发送数据时,sweetalert 只出现不到一秒钟,然后消失,无论用户选择如何发送表单。

我如何修改我的代码,当我想要定期的窗口确认时,当用户点击“删除”时,表单将被发送,否则当他点击“取消”时,表单将不送吗?

if(isset($_POST['submit'])) {  
  echo 'Your form was submitted.';
}

<form action="#" method="post>                
 <label>
   <span>Username</span>
   <input type="text" value="" name="username" />
 </label>
 <input type="submit" name="submit" value="Register!" id="test-1" />    
</label>     
</form>



<script>
 document.querySelector('#test-1').onclick = function(){
  swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
  }).then(function () {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
      )
  })

     //IF SWAL => DELETE -> send form
     //ELSE IF SWAL => CANCEL -> do nothing 
     //when I put here "return false;", then sweetalert is working, but it doesn't send the form
</script>

感谢新手的解决方案:

<?php if(isset($_POST['send']))
{echo 'Your form was submitted.';} ?>

<form id='myfrm' action="#" method="post">   
 <label>
   <span>Username</span>
   <input type="text" value="" name="username" />
   <input type="hidden" name="send" value="send">
 </label>
</form>
 <button value="Register!" id="test-1"> submit </button>

<script>
document.querySelector("#test-1").onclick = function(){
  swal({
    title: "Are you sure?",
    text: "You won't be able to revert this!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#3085d6",
    cancelButtonColor: "#d33",
    confirmButtonText: "Yes, delete it!"
  }).then(function () {
   document.querySelector("#myfrm").submit();    
  })
}      
</script>

【问题讨论】:

    标签: javascript jquery sweetalert sweetalert2


    【解决方案1】:

    您可以在表单上使用处理程序而不是使用处理程序,这样会更好

    给表单一个id 也将您的submit 按钮更改为普通按钮,因为我们将根据用户的选择手动提交(或不提交)

    <form id='myfrm' action="#" method="post">    <!-- u missed quote here -->            
     <label>
       <span>Username</span>
       <input type="text" value="" name="username" />
     </label>
     <!-- EDIT : removed (not working)
     <input type="button" name="submit" value="Register!" id="test-1" />    
    -->
    <input type="hidden" name="submit" value="submit"/>
    </label>     
    </form>
    <!-- EDIT : added -->
    <button name="submit" value="Register!" id="test-1"> submit </button>
    
    
    
    <script>
     document.querySelector('#test-1').onclick = function(){
      swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
      }).then(function () {
    
         // swal( 'Ready?!', 'Your form is going to be submitted.' 'success');
         // if you want to show alert that form is going to submit you may un-comment above line
        document.querySelector('#myfrm').submit(); // manully submit 
      })
    

    【讨论】:

    • 谢谢你,新手!但它仍然不起作用(或者只是从部分 - 它成功询问用户是否要删除它;但 .then(function() 中的第二部分不起作用):avcr1.chudst.cz/test.php
    • 您可能使用的是旧版本的 sa2 或 sa1,它们在选项中使用回调函数,因此基于 promise 的 (then()) 语法在这些版本中不可用请参阅 here 它应该如何工作在最新版本上
    • 我想,我使用的是最新版本(6.2.0)。你是对的,“警报”是可以的,所以问题不在于“.then(function()”,而是直接在“document.querySelector('#myfrm').submit();” - 的表单仍然没有发送数据。
    • 你给action值了吗?
    • 不,因为我在同一页面上有 if(isset($_POST['submit']){},所以我在 # 上留下了操作值 'set'。
    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2014-09-07
    • 2012-07-17
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多