如何停止 Actionlink 重定向并使用 javascript 进行弹出和重定向?

发布于 2024-09-16 17:24:07 字数 813 浏览 8 评论 0原文

这是我当前拥有的脚本 -

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

现在这是无论发生什么情况,actionlink 都会重定向的问题,我需要停止它。我的意思是说用户按下取消按钮确认操作链接仍然执行并转到RemoveSystem Action,我希望它只保留在页面上。我想过使用 HTML 超链接,但我不知道应该如何将 id 值传递给在这种情况下的 javascript 。有人可以帮我吗?

This is script what I have currently -

<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> 

Here is the link which is there for each record and we can delete each system when we click on its delete button -

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

Now here is the problem the actionlink redirects no matter what happens and I need to stop it. I mean say the user presses cancel on the confirm the actionlink still executes and goes to RemoveSystem Action, where I want it to just stay on the page.I thought of using a HTML hyperlink but I dont know how I should pass id the value to the javascript in that case. Can anyone please help me out ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神魇的王 2024-09-23 17:24:07

您需要阻止链接的默认操作,如下所示:

$('.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 ...这是您需要防止发生的行为。

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.

猫七 2024-09-23 17:24:07

如果要阻止默认操作,请返回 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();
});

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();
});
一口甜 2024-09-23 17:24:07

在您的脚本函数中添加:

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

In your script function add:

if (answer)  
    window.location = href;
else
   return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文