【问题标题】:How to submit a form with bootbox confirmation如何提交带有 bootbox 确认的表单
【发布时间】:2017-03-03 08:16:11
【问题描述】:

我有一个提交复选框值的表单。在提交之前,会显示 Bootbox 确认对话框。但是,当单击“是”时,表单不会提交。我该如何解决?我的代码是:

$('#student_delete_form').submit(function(e) {
  var currentForm = this;
  e.preventDefault();
  bootbox.confirm("Are you sure?", function(result) {
    if (result) {
      currentForm.submit();
    }
  });
});
<form id="student_delete_form" name="" action="#" method="post"></form>
<input type='submit' value='Delete' name='delete' form="student_delete_form"><br>
<?php $studentArray = array(3, 4, 5, 6); ?>
<?php foreach ($studentArray as $key => $value): ?>
<input name="checkbox[]" type="checkbox" value="<?php echo $value;?>" form="student_delete_form">
<?php echo $value;?><br>
<?php endforeach; ?>

<?php
    if(isset($_POST['delete'])){

      $chk=isset($_POST['checkbox'])? $_POST['checkbox']:"";
      if ($chk != "")
      {
        $chk_array=array_filter($chk);

        foreach($chk as $key => $chke)
        {
          echo "$chke";
        }
      }
    }
?>

【问题讨论】:

  • 你处于一个无限循环中——再次调用同一个函数只会让你回到同一个地方。然后,您需要调用不同的函数,或者将参数传递给该函数,告诉自己用户单击了确定,所以现在提交

标签: jquery forms bootbox


【解决方案1】:

您处于无限循环中 - 再次调用同一个函数只会让您回到同一个地方。

你可以试试下面的javascript

$("input[name='delete']").on("click",function(e) {
  e.preventDefault();
  bootbox.confirm("Are you sure?", function(result) {
    if (result) {
      $('#student_delete_form').submit();
    }
  });
});

【讨论】:

  • 那些只是给出对话确认循环或根本不提交
  • 没关系 :) 但实际上我已经尝试过更新代码,但它也不起作用。
【解决方案2】:

就这样使用确认,试试这个(未测试但应该接近)

$("input[name='delete']").click(function() {
  bootbox.confirm("Are you sure?", function(result) {
    if (result) {
      $('#student_delete_form').submit();
    }
  });
});

更新

从“删除”按钮中删除 form="student_delete_form"

更新 2

如果您不想从“删除”按钮中删除表单属性:

$("input[name='delete']").click(function(e) {
  e.preventDefault();
  bootbox.confirm("Are you sure?", function(result) {
    if (result) {
      $('#student_delete_form').submit();
    }
  });
});

Here's a fiddle showing the code working as it should

【讨论】:

  • 如果删除 e.preventDefault(); , Bootbox 确认对话框将不会显示。
  • 我在答案中添加了一个小提琴,一切都按计划进行,所以可能是其他原因阻止它
  • 只显示警报很好,但不能在提交的表单中传递值。或者,我必须添加另一个提交函数来传递 PHP 值?顺便说一句,我做了解决方法以获得我想要的结果。无论如何,谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2011-09-23
相关资源
最近更新 更多