如何将变量传递给回调函数?

时间:2013-12-26 18:10:05

标签: jquery jquery-callback

请参阅代码:

$('.delete_group').click(function(e) {
   var group_id = $(this).attr('placeholder');
   e.preventDefault();
   confirm("Are you sure you want to delete this Group?", 
      function deleteGroup(XXX) {
         window.location.href = '<?php echo base_url(); ?>user/deleteGroupById/' + group_id;
      }
   );
});

我需要将group_id传递到XXX位置。

谢谢大家。

3 个答案:

答案 0 :(得分:0)

window.confirm将返回一个布尔值。 True如果用户选择“确定”,则为False

if(confirm("Are you sure you want to delete this Group?"))
{
  //User clicked "OK". Do the deletion code
}
else
{
  //User clicked cancel.
}

答案 1 :(得分:0)

function deleteGroup(group_id) {
    window.location.href = '<?php echo base_url(); ?>user/deleteGroupById/' + group_id;
}

$('.delete_group').click(function(e) {
   var group_id = $(this).attr('placeholder');
   e.preventDefault();
   if (confirm("Are you sure you want to delete this Group?")) {
      deleteGroup(group_id);
   }
});

答案 2 :(得分:0)

我认为应该是:

$('.delete_group').click(function (e) {
    var group_id = $(this).attr('placeholder');
    e.preventDefault();
    var answer = confirm("Are you sure you want to delete this Group?");
    if (answer) {
        deleteGroup(group_id);
    }
});

function deleteGroup(XXX) {
    window.location.href = '<?php echo base_url(); ?>user/deleteGroupById/' + XXX;
}