正在开发 MVC 5 应用程序。
我有一个按钮定义为...
@Html.ActionLink("Delete Chart " + Model[0].CurrentChartNumber,
"DeleteChartData",
new { eventId = Model[0].EventId,
chartNumber = Model[0].CurrentChartNumber },
new { @class = "btn btn-danger btnDelete" })
它被渲染为...
<a class="btn btn-danger btnDelete"
href="/AuditResults/DeleteChartData?eventId=50&chartNumber=1">
Delete Chart 1
</a>
我正在使用sweetAlert用于删除确认。一切正常,但是,在用户单击"is"后,我无法让它导航到定义的 href。这是 JavaScript 代码...
<script>
$(function() {
$(".btnDelete").click(function(e) {
e.preventDefault();
swal({
title: 'Are you sure you want to DELETE this chart?',
text: "You won't be able to undo this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!'
}).then(function() {
//user selected "Yes"
return true;
});
});
})
</script>
因此,如果用户单击"is",它应该导航到...
/AuditResults/DeleteChartData?eventId=50&chartNumber=1
...但它无法导航到任何地方。 (我什至删除了 e.preventDefault() 并尝试了它。)知道我做错了什么吗?谢谢!
最佳答案
当用户选择 yes 时,您除了从 promise 中返回 true 之外什么都不做。因此,请尝试在代码块内添加重定向到您想要的页面。
<script>
$(function() {
$(".btnDelete").click(function(e) {
e.preventDefault();
swal({
title: 'Are you sure you want to DELETE this chart?',
text: "You won't be able to undo this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!'
}).then(function() {
//user selected "Yes"
window.location.replace("AuditResults/DeleteChartData?eventId=50&chartNumber=1");
return true;
});
});
})
</script>
关于javascript - 确认后导航至 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43747942/