如何使用bootbox确认提交表单

时间:2017-03-03 08:16:11

标签: jquery forms bootbox

我有一个提交复选框值的表单。在提交之前,将显示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";
        }
      }
    }
?>

2 个答案:

答案 0 :(得分:0)

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

您可以尝试关注javascript

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

答案 1 :(得分:0)

使用确认就是这样,试试这个(未经测试但应该关闭)

$("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