简体   繁体   English

Javascript 确认删除

[英]Javascript confirm delete

Am creating a simple cms and i have the following我正在创建一个简单的 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>

The above code is wrapped in a form tag上面的代码被包裹在一个表单标签中

This my javascript code这是我的 javascript 代码

function delete_confirm() {
    var msg = confirm('Are you sure you want to delete the selected article(s)');
    if(msg == false) {
        return false;
    }
}

This is delete function in my controller这是在我的 controller 中删除 function

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");
     }

But when cancel is clicked in the confirm dialog the form still is still submited and the selected article deleted.但是,当在确认对话框中单击取消时,表单仍然被提交并且所选文章被删除。 Please advice on what to do请指教该怎么做

Try changing尝试改变

<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="delete_confirm()" />

to

<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="return delete_confirm();" />

You're running the delete_confirm() function inside of the onclick function.您正在 onclick function 内部运行 delete_confirm() function。 you need to return false from the onclick() function itself.您需要从 onclick() function 本身返回 false 。

try onSubmit instead of onClick .尝试onClick onSubmit

What you'll want to do is something like this:你想要做的是这样的:

<form onsubmit="return delete_confirm();" ...>
  <input ...>
</form>

When the form is submitted via the type="submit" -button, your delete_confirm() -function is called.当表单通过type="submit" -button 提交时,您的delete_confirm()被调用。 If it returns true , the form is submitted.如果返回true ,则提交表单。 If it returns false , it's not.如果它返回false ,则不是。

Try using AJAX to call the PHP script from within Javascript.尝试使用 AJAX 从 Javascript 中调用 PHP 脚本。

This means set the input button type to button <input type="button" /> and change the onclick such that if cancel is pressed, return false (like what you did) or if OK is pressed, run the AJAX script and redirect the page if required...这意味着将输入按钮类型设置为按钮<input type="button" />并更改 onclick 以便如果按下取消,返回 false(就像你所做的那样)或者如果按下 OK,运行 AJAX 脚本并重定向如果需要页面...

You could write the AJAX server-side (PHP) script in a seperate file, and send in the paramters using $_GET.您可以在单独的文件中编写 AJAX 服务器端 (PHP) 脚本,并使用 $_GET 发送参数。

delete_confirm() must always return false in your case.在您的情况下, delete_confirm()必须始终返回false

You want the delete confirm function to be your onsubmit attribute of the form.您希望删除确认 function 成为表单的onsubmit属性。 Having it on the input element is not enough.将它放在输入元素上是不够的。

Maybe try to use:也许尝试使用:

<a href="javascript: if(confirm('Relly?')) { document.getElementById('your_form').action='php.php'; document.getElementById('your_form').submit(); } ">Delete</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM