How do I stop an Actionlink redirect and instead popup and redirect using javascript?

半城伤御伤魂 提交于 2019-12-08 09:11:28

You need to prevent the default action of the link like this:

$('.RemoveSystem').click(function () {
  if (confirm("Are you sure you want to delete this system?")) 
    window.location = $(this).attr('href');
  return false;
});

Or this:

$('.RemoveSystem').click(function (e) {
  if (confirm("Are you sure you want to delete this system?")) 
    window.location = $(this).attr('href');
  e.preventDefault();
});

Currently, the link is doing this function but also doing what it does with no JavaScript at all, which is to go to the href it has...that's the behavior you need to prevent from happening.

Return false if you want to prevent the default action:

$('.RemoveSystem').click(function () {
    var href = $(this).attr('href');
    var answer = confirm("Are you sure you want to delete this system?");
    if (answer) 
        window.location = href;
    return false;
});

or use preventDefault:

$('.RemoveSystem').click(function (evt) {
    var href = $(this).attr('href');
    var answer = confirm("Are you sure you want to delete this system?");
    if (answer) 
        window.location = href;
    evt.preventDefault();
});

In your script function add:

if (answer)  
    window.location = href;
else
   return false;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!