2010-09-01 84 views
1

这是脚本我有什么现在 -如何停止Actionlink重定向并改为使用JavaScript弹出和重定向?

<script type="text/javascript"> 
     $(document).ready(function() { 
      $('.RemoveSystem').click(function() { 
       var href = $(this).attr('href'); 
       var answer = confirm("Are you sure you want to delete this system?"); 
       alert(answer); 
       if (answer) 
        window.location = href; 
      }); 
     }); 
    </script> 

这里是一个有每条记录的链接,我们可以删除每个系统当我们点击了删除按钮 -

<%= Html.ActionLink("Delete", "RemoveSystem", new { SysNum = item.Id}, new { @class = "RemoveSystem" })%> 

现在这里无论发生什么事情,动作链接都会重定向,我需要阻止它。我的意思是说,用户按确认动作链接仍然执行并进入RemoveSystem行动,我希望它留在页面上。我想使用HTML超链接,但我不知道我应该如何通过身份证的价值在这种情况下的JavaScript。任何人都可以请帮我吗?

如果要防止默认动作

回答

5

您需要防止这样的链接的默认行动:

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

或者这样:

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

目前,该链接是这样做的功能,但也做什么,没有的JavaScript可言,这是去它具有href做......这就是你需要的发生,防止行为。

2

返回false:

$('.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; 
}); 

或使用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(); 
}); 
1

在你的脚本功能添加:

if (answer) 
    window.location = href; 
else 
    return false;