我的Html代码是,
<a title="Delete" onclick="if (confirm('Delete selected item?')){ return true; }else{ event.stopPropagation(); event.preventDefault();};" class="delete" href="link to delete page">
</a>
我希望根据某些条件提供警报功能而不是此警报,并且在该用户确认该选项后,它会转到删除页面。
我的Jquery代码是,
$('.delete').removeAttr('onclick').click(function(e) {
e.preventDefault();
var url = $(this).attr("href");
var a = url.split('id_product=');
var next = a[1];
var id=next.split('&');
var base_url = $("#ajaxbase").val();
$.ajax({
type : "POST",
url : base_url+"modules/sample/ajax/sample.php",
data: {id:id[0]}
}).done( function( response ) {
var res=$.trim(response);
if ( res == '1' ) {
if ( confirm('This Product is configured in chain.Deleting this product will affect your chain.Are you sure to do this?') ) {
return true;
} else {
return false;
}
} else if ( res == '2' ) {
if ( confirm('Are you sure to delete this product?') ) {
return true;
} else {
return false;
}
}
});
显示我的确认消息,但如果用户确认,则不会进入删除页面。谁能帮助我。
答案 0 :(得分:0)
您可以通过更好地控制非<a>
锚元素来实现此目的。通常很好,如果你希望完全控制给定元素的行为,我也认为它只是更清洁。
重要的是要注意,最后的window.location.href
继续进行原始的缩进行为。
<span class="delete" data-linkto="somepage.php">Delete</span>
<script>
$('.delete').on('click', function() {
var
url = $(this).data("linkto"),
a = url.split('id_product='),
next = a[1],
id = next.split('&'),
base_url = $("#ajaxbase").val(),
verifyIfOne = confirm('This Product is configured in chain. '+
'Deleting this product will affect your chain. '+
'Are you sure to do this?'),
verifyIfTwo = confirm('Are you sure to delete this product?'),
Accepted = false;
$.ajax({
//Headers
type : "POST",
url : base_url + "modules/sample/ajax/sample.php",
data: { id:id[0] }
}).done( function( response ) {
var res = $.trim(response);
if ( res == '1' ) {
if ( verifyIfOne ) {
Accepted = true;
}
}
if ( res == '2' ) {
if ( verifyIfTwo ) {
Accepted = true;
}
}
/**
* User has agreed to go through with
* the deletion.
**/
if ( Accepted )
window.location.href = url;
});
</script>
答案 1 :(得分:0)
你需要在ajax调用之后添加return false并在ajax调用中处理结果不同因为ajax调用是asyn而因此它不会等待它返回true / false
编辑这可能就是您要找的内容
$('.delete').removeAttr('onclick').click(function(e) {
e.preventDefault();
var url = $(this).attr("href");
var a = url.split('id_product=');
var next = a[1];
var id=next.split('&');
var base_url = $("#ajaxbase").val();
$.ajax({
type : "POST",
url : base_url+"modules/sample/ajax/sample.php",
data: {id:id[0]}
}).done( function( response ) {
var res=$.trim(response);
if ( res == '1' ) {
if ( confirm('This Product is configured in chain.Deleting this product will affect your chain.Are you sure to do this?') ) {
window.location.href = url ;
}
} else if ( res == '2' ) {
if ( confirm('Are you sure to delete this product?') ) {
window.location.href = url ;
}
}
return false;
});