2012-04-25 31 views
29

Trong jQuery tôi đang hiển thị kết quả của tôi trong một định dạng bảng đầu ra, một phần của jQuery của tôi làjQuery xóa hộp xác nhận

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

đây "close" là một lớp CSS và trong lớp học này tôi có hình ảnh đánh dấu chéo, nếu Tôi nhấp vào dấu chéo này chức năng (deleteItem) sẽ được bắn.

ở đây những gì tôi muốn làm là nếu tôi nhấp vào dấu chéo này, "delete confirmation box" sẽ bật lên và nếu tôi nhấp vào có sự kiện onclick này sẽ kích hoạt, nếu không nhấp Không có gì xảy ra.

Làm thế nào tôi có thể đạt được điều này, bất cứ ai có thể giúp tôi ....

Trả lời

96

Bạn cần phải thêm xác nhận() để deleteItem của bạn();

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

này hoạt động, tuy nhiên cần lưu ý rằng đây không phải là jQuery cụ thể và chỉ là Vanilla JavaScript. – crmpicco

7

Hãy thử với mã bên dưới:

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

HOẶC

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

Điều này có thể làm việc cho bạn ..

Cảm ơn.

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

Bạn cũng có thể sử dụng jquery modalin cùng cách phiên bản

JQuery

Bạn có chắc chắn?
$(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

Hãy thử với JSFiddle DEMO này: 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

cảm ơn bạn đã trả lời có giá trị với một ví dụ, tôi đã nhận được giải pháp – shanish

3

Đơn giản chỉ cần hoạt động như:

$("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

Cập nhật JQuery cho phiên bản 1.9.1 link để xóa là here$("#div1").find('button').click(function(){...}

7

Tôi sử dụng này:

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

thử này bạn tôi

// 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>

Các vấn đề liên quan