向此代码添加确认

adding a confirm to this code?

本文关键字:确认 添加 代码      更新时间:2023-09-26

这是我的jquery

$('.delete_step').live('click', function(e) {
e.preventDefault();
  var delete_location = window.location.pathname.replace('admin/', '') + '?route=module/cart/delete_step';
  $.post( delete_location,  { step_id: $(this).attr("rel"), template_number: "<?php print $template_id; ?>" },
       function(result) {
            var token = window.location.search.match(/token=('w+)/)[1];
           window.location.href = window.location.pathname + '/index.php?route=system/template&token=' + token;
  });
});

这是我的HTML

<span class="delete"><a rel="<?php print $step['step_number']; ?>" class="delete_step" href="#">Delete Step</a></span>  

如何在此周围添加确认是/否对话框....任何想法

使用window.confirm()

$('.delete_step').live('click', function(e) {
    e.preventDefault();
    if (confirm('Are you sure?')) {
        // do the $.post()
    }
}

类似这样的内容(未测试)

$('.delete_step').live('click', function(e) {
    e.preventDefault();
    var answer = confirm("Are you sure?")
    if (answer){
        var delete_location = window.location.pathname.replace('admin/', '') + '?route=module/cart/delete_step';
        $.post( delete_location,  { step_id: $(this).attr("rel"), template_number: "<?php print $template_id; ?>" },
       function(result) {
            var token = window.location.search.match(/token=('w+)/)[1];
           window.location.href = window.location.pathname + '/index.php?route=system/template&token=' + token;
       });
   }else{
       alert('fail!');
   }
    });
$('.delete_step').live('click', function(e) {e.preventDefault ();if(confirm("要弹出的消息?")){Var delete_location = window.location.pathname。替换('admin/', ') + '?route=module/cart/delete_step';美元。Post (delete_location, {step_id: $(this).attr("rel"), template_number: "},函数(结果){Var token= window.location.search.match(/token=('w+)/)[1];Window.location.href = window.location.pathname + '/index.php?Route =system/template&token=' + token;});}});

这将在继续执行函数之前询问他们:

$('.delete_step').live('click', function(e) {
    e.preventDefault();
    var response = confirm("Are you sure you want to delete this?");
    if(response){
        var delete_location = window.location.pathname.replace('admin/', '') + '?route=module/cart/delete_step';
        $.post( delete_location,  { step_id: $(this).attr("rel"), template_number: "<?php print $template_id; ?>" },
            function(result) {
                var token = window.location.search.match(/token=('w+)/)[1];
                window.location.href = window.location.pathname + '/index.php?route=system/template&token=' + token;
        });
    }
});