如何停止 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" })%>
现在这是无论发生什么情况,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要阻止链接的默认操作,如下所示:
或者这样:
当前,该链接正在执行此功能,但也根本不需要JavaScript即可执行其操作,这是转到它所具有的
href
...这是您需要防止发生的行为。You need to prevent the default action of the link like this:
Or this:
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.如果要阻止默认操作,请返回 false:
或使用 preventDefault:
Return false if you want to prevent the default action:
or use preventDefault:
在您的脚本函数中添加:
In your script function add: