在此代码中添加确认?

时间:2011-04-25 17:10:59

标签: javascript jquery jquery-ui

这是我的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>  

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

4 个答案:

答案 0 :(得分:3)

使用window.confirm()

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

答案 1 :(得分:1)

像这样(未经测试)

$('.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!');
   }
    });

答案 2 :(得分:1)

$('.delete_step').live('click', function(e) {
    e.preventDefault();

       if(confirm("Message to be popped up?"))
       {
      var delete_location = window.location.pathname.replace('admin/', '') + '?route=module/cart/delete_step';
      $.post( delete_location,  { step_id: $(this).attr("rel"), template_number: "" },
           function(result) {
                var token = window.location.search.match(/token=(\w+)/)[1];
               window.location.href = window.location.pathname + '/index.php?route=system/template&token=' + token;
      });
       }
    });

答案 3 :(得分:0)

在继续你的功能之前,这会问他们:

$('.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;
        });
    }
});