2017-03-03 33 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

對不起,我已經嘗試過,並沒有提交表格。 –