2017-03-03 114 views
0

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

+1

你在无限循环 - 调用同一个功能再次只会让你回到同一个地方。然后你需要调用一个不同的函数,或者将一个参数传递给那个告诉自己用户点击好的函数,所以现在只需提交 –

回答

0

你在无限循环 - 再次调用同一功能只返回你回到同一个地方。

你可以试试下面的JavaScript

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

那些只是给出对话确认的循环或者根本没有提交 –

+0

表单提交仍然不工作 –

+0

这很好:)但实际上我已经尝试过更新的代码,并且它也不会工作。 –

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

+0

if remove e.preventDefault(); ,Bootbox确认对话框将不会显示。 –

+0

看看更新 –

+0

对不起,我已经尝试过,并没有提交表格。 –