我正在创建一个简单的 cms,我有以下内容
<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="delete_confirm()" />
<tbody>
<?php foreach ($articles as $article): ?>
<tr>
<td align="center">
<input id="article_id"name="article[]" type="checkbox" value="<?php echo $article->id; ?>" /></td>
<td align="center"><?php echo anchor('admin/articles/edit/'.$article->id,$article->title); ?></td>
<td align="center"><?php echo $article->author; ?></td>
<td align="center"><?php echo $article->category; ?></td>
<td align="center"><?php published($article->post_status); ?></td>
<td align="center"><?php featured($article->featured); ?></td>
<td align="center"><?php echo $article->views; ?></td>
<td align="center"><?php echo $article->post_date; ?></td>
</tr>
<?php endforeach;?>
</tbody>
上面的代码包裹在一个表单标签中
这是我的javascript代码
function delete_confirm() {
var msg = confirm('Are you sure you want to delete the selected article(s)');
if(msg == false) {
return false;
}
}
这是我 Controller 中的删除功能
function articles_delete() {
//Load Model
$this->load->model("article_model");
//Assign article id to variable
$checkbox = $_POST['article'];
//loop through selected checkbox
for($i = 0; $i < count($checkbox); $i++) {
//Call delete method of the article model
$this->article_model->delete($checkbox[$i]);
}
$this->session->set_flashdata("message","Article(s) Deleted");
redirect("admin/articles","refresh");
}
但是当在确认对话框中单击取消时,表单仍然被提交并且所选文章被删除。 请指点一下该怎么做
最佳答案
尝试改变
<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="delete_confirm()" />
到
<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="return delete_confirm();" />
您正在 onclick 函数内部运行 delete_confirm() 函数。您需要从 onclick() 函数本身返回 false。
关于php - Javascript 确认删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720062/