2012-04-25 99 views
29

在我的jQuery我显示在一个表格式的输出我的结果,我的jQuery的一部分jQuery的删除确认对话框

<td class='close' onclick='deleteItem(#ITEMID,#ITEMNAME)'></td> 

这里"close"是一个CSS类,并在这个类我有十字标记的图像,如果我点击这个十字标记的功能(deleteItem)将被解雇。

这里就是我想要做的是,如果我点击这个十字标记"delete confirmation box"应该弹出,如果我点击是的,这onclick事件应该火,否则如果我点击没有什么应该发生。

我怎样才能做到这一点,任何人都可以帮助我....

回答

96

您需要确认()添加到您的deleteItem();

function deleteItem() { 
    if (confirm("Are you sure?")) { 
     // your deletion code 
    } 
    return false; 
} 
+4

这工作,然而,应该指出的是,这并不是jQuery的特定的,仅仅是香草的JavaScript。 – crmpicco 2013-05-28 11:20:00

7

尝试用下面的代码:

$('.close').click(function(){ 
var checkstr = confirm('are you sure you want to delete this?'); 
if(checkstr == true){ 
    // do your code 
}else{ 
return false; 
} 
}); 

OR

function deleteItem(){ 
    var checkstr = confirm('are you sure you want to delete this?'); 
    if(checkstr == true){ 
     // do your code 
    }else{ 
    return false; 
    } 
    } 

这可能会为你工作..

感谢。

8
function deleteItem(this) { 
    if (confirm("Are you sure?")) { 
      $(this).remove(); 
    } 
    return false; 
} 

您还可以使用jQuery modalin同样的方式

JQuery的版本

你确定吗?
$(document).ready(function() { 
    $("#dialog-box").dialog({ 
     autoOpen: false, 
     modal: true 
    }); 

    $(".close").click(function(e) { 
    var currentElem = $(this); 
    $("#dialog-box").dialog({ 
     buttons : { 
     "Confirm" : function() { 
      currentElem.remove() 
     }, 
     "Cancel" : function() { 
      $(this).dialog("close"); 
     } 
     } 
    }); 

    $("#dialog-box").dialog("open"); 
    }); 
}); 
4

尝试用这种JSFiddle DEMO:http://jsfiddle.net/2yEtK/3/

Jquery Code: 

$("a.removeRecord").live("click",function(event){ 
    event.stopPropagation(); 
    if(confirm("Do you want to delete?")) { 
    this.click; 
     alert("Ok"); 
    } 
    else 
    { 
     alert("Cancel"); 
    }  
    event.preventDefault(); 

}); 
+0

感谢您通过一个例子的宝贵回应,我得到了解决方案 – shanish 2012-04-25 06:48:39

3

简单的工作原理:

$("a. close").live("click",function(event){ 
    return confirm("Do you want to delete?"); 
}); 
25
$(document).ready(function(){ 
    $(".del").click(function(){ 
    if (!confirm("Do you want to delete")){ 
     return false; 
    } 
    }); 
}); 
2

更新的JQuery的版本1.9.1链接,删除是here$("#div1").find('button').click(function(){...}

7

我用这个:

<a href="url/to/delete.asp" onclick="return confirm(' you want to delete?');">Delete</a> 
0

试试这个我的朋友

// Confirmation Message On Delete Button. 
 

 
    $('.close').click(function() { 
 
    return confirm('Are You Sure ?') 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<td class='close'></td>