代码之家  ›  专栏  ›  技术社区  ›  Vishal

如何使用javascript停止Actionlink重定向而不是弹出和重定向?

  •  1
  • Vishal  · 技术社区  · 14 年前

    这是我目前的剧本-

    <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" })%>
    

    3 回复  |  直到 14 年前
        1
  •  6
  •   Nick Craver    14 年前

    你需要防止

    $('.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
  •  2
  •   Darin Dimitrov    14 年前

    如果要阻止默认操作,请返回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();
    });
    
        3
  •  1
  •   Kelsey    14 年前

    在脚本函数中添加:

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