如何在提交前添加确认框

时间:2017-05-17 12:08:27

标签: javascript php jquery ajax wordpress

我是新开发者,我遇到了问题。我想你可以解决这个问题。我想在删除前显示确认框。我怎么能这样做我的Ajax代码 的 HTML:

<input type='button' class="btn btn-danger delete_button" id="delete_button<?php echo $row->id;?>" value="delete" onclick="delete_row('<?php echo $row->id;?>');">

AJAX

function delete_row(id)
{
 jQuery.ajax({
    type:'post',
    url: "/blue_bucket/wp-admin/admin-ajax.php?action=delete_form",  
    data:{
            delete_row:'delete_row',
            row_id:id,
        },
    success:function(response) {
        if(response=="success")
        {
            var row=document.getElementById("row"+id);
            row.parentNode.removeChild(row);
        }
        top.location.href="admin.php?page=data_list";
    }
 });
}

Wordpress PHP

function delete_form()
    {
        if(isset($_POST['delete_row']))         
        {
            $id=$_POST['row_id'];
            echo $row_no;
            global $wpdb;
            $table_name = "countries";
            $wpdb->delete($table_name, 
                        array( 'id' => $id ) );
            exit();
        }    
    }

add_action( 'wp_ajax_delete_form', 'delete_form' );
add_action('wp_ajax_nopriv_delete_form', 'delete_form');

4 个答案:

答案 0 :(得分:3)

你可以在ajax调用之前加上confirmmbox,见下面的代码

function delete_row(id)
{
if (confirm("Do you want to delete?") == true) {
 jQuery.ajax({
    type:'post',
    url: "/blue_bucket/wp-admin/admin-ajax.php?action=delete_form",  
    data:{
            delete_row:'delete_row',
            row_id:id,
        },
    success:function(response) {
        if(response=="success")
            {
            var row=document.getElementById("row"+id);
            row.parentNode.removeChild(row);
            }
            top.location.href="admin.php?page=data_list";
    }
 });
}
}

答案 1 :(得分:1)

尝试使用confirm方法

function delete_row(id)
{
if(!confirm('Are you sure to delet')){
return false;
}
 jQuery.ajax({
    type:'post',
    url: "/blue_bucket/wp-admin/admin-ajax.php?action=delete_form",  
    data:{
            delete_row:'delete_row',
            row_id:id,
        },
    success:function(response) {
        if(response=="success")
            {
            var row=document.getElementById("row"+id);
            row.parentNode.removeChild(row);
            }
            top.location.href="admin.php?page=data_list";
    }
 });
}

答案 2 :(得分:0)

将此作为第一行添加到delete_row():

if(confirm('Are you sure you want to delete?')) { // code inside this... }

答案 3 :(得分:0)

看看我的代码: https://github.com/delboy1978uk/bs-delete-confirm

如果您在项目中使用Bootstrap(编辑 - 我只是注意到你!:-D),请按照README.md进行操作!只需要在确认模式连接的按钮上添加一个类。如果没有,请查看实际代码,并将HTML调整为任何内容,然后编辑javascript调用或弹出对话框:

(function($) {
    $.fn.extend({
        deleteConfirm: function(options)
        {
            var defaults =
            {
                heading: "Please confirm",
                body: 'Are you sure you wish to perform this action?',
                ok_text: 'Proceed',
                cancel_text: 'Back',
                log: false
            };

            options = $.extend(defaults,options);

            $('body').append('<div class="modal fade" id="bs-delete-confirm" tabindex="-1" role="dialog" aria-labelledby="bs-delete-confirm-label" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title" id="bs-delete-confirm-label">Please confirm</h4></div><div id="bs-delete-confirm-body" class="modal-body"></div><div class="modal-footer"><a id="bs-delete-confirm-cancel" class="btn btn-default" data-dismiss="modal" aria-hidden="true"></a><a id="bs-delete-confirm-ok" class="btn btn-primary"></a></div></div></div></div>');

            $('#bs-delete-confirm-label').html(options.heading);
            $('#bs-delete-confirm-body').html(options.body);
            $('#bs-delete-confirm-cancel').html(options.cancel_text);
            $('#bs-delete-confirm-ok').html(options.ok_text);

            this.each(function() {
                $(this).on('click',function(e){
                  e.preventDefault();
                  link = $(this).prop('href');
                  $('#bs-delete-confirm-ok').prop('href',link);
                  $('#bs-delete-confirm').modal();
                  if(options.log == true){
                      console.log('Clicked link = ' + link);
                  }
                });
            });
        }
    });
})(jQuery);