jQuery删除确认框

sha*_*ish 36 jquery

在我的jQuery中,我在一个表格格式的输出中显示我的结果,我的jQuery的一部分是

<td class='close' onclick='deleteItem(#ITEMID,#ITEMNAME)'></td>
Run Code Online (Sandbox Code Playgroud)

这里"close"是一个CSS类,在这个类中我有十字标记图像,如果我点击这个十字标记,函数(deleteItem)将被触发.

在这里我想要做的是如果我点击这个十字标记"delete confirmation box"应该弹出,如果我点击是这个onclick事件应该触发,否则如果我点击否任何事都应该发生.

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

Mār*_*ovs 109

你需要在你的deleteItem()中添加confirm();

function deleteItem() {
    if (confirm("Are you sure?")) {
        // your deletion code
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是有效的,但应该注意的是,这不是特定于jQuery的,只是vanilla JavaScript. (6认同)

Sha*_*gde 26

$(document).ready(function(){
  $(".del").click(function(){
    if (!confirm("Do you want to delete")){
      return false;
    }
  });
});
Run Code Online (Sandbox Code Playgroud)


Cha*_*h M 10

试试以下代码:

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

要么

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

这可能对你有用..

谢谢.


Uma*_*mid 10

我用过这个:

<a href="url/to/delete.asp" onclick="return confirm(' you want to delete?');">Delete</a>
Run Code Online (Sandbox Code Playgroud)


Hüs*_*BAL 8

function deleteItem(this) {
    if (confirm("Are you sure?")) {
          $(this).remove();
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

你也可以用同样的方式使用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");
  });
});
Run Code Online (Sandbox Code Playgroud)