javascript - 如何在asp.net mvc中的html.actionlink中调用javascript确认对话框函数?

标签 javascript jquery asp.net asp.net-mvc asp.net-mvc-4

当用户单击删除按钮时,我想显示确认对话框菜单。

但是按钮不会触发我的脚本。触发 JavaScript 后,如果用户选择“确定”,脚本必须执行该操作。否则,如果用户单击“取消”,则不会发生任何情况。

这是 JavaScript 代码;

<script type="text/javascript">
    $(document).ready(function () {
        $(".confirmDialog").on("click", function (e) {
            // e.preventDefault(); use this or return false
            var url = $(this).attr('href');
            $("#dialog-confirm").dialog({
                autoOpen: false,
                resizable: false,
                height: 170,
                width: 350,
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: true,
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                        window.location = url;
                    }, "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });
            $("#dialog-confirm").dialog('open');
            return false;
        });
    });
</script>

这是MVC代码;

<div style="text-align: right; font-weight: bold; font-size: larger; color: red;">
    <tr>
        <td>@Html.ActionLink("Delete", "Your-Action", new { @class = "confirmDialog" })</td>
    </tr>
</div>
<div id="dialog-confirm" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        Are you sure ?
    </p>
</div>

最佳答案

您使用的 Actionlink 重载需要将第三个参数作为对象路由而不是 htmlattributes

正确的操作链接如图所示:

@Html.ActionLink("Delete", "Your-Action", new{} ,new { @class = "confirmDialog" })

或者

@Html.ActionLink("Delete", "Your-Action", null ,new { @class = "confirmDialog" })

你的其余代码都很好..!!

关于javascript - 如何在asp.net mvc中的html.actionlink中调用javascript确认对话框函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120008/

相关文章:

javascript - 您如何以编程方式确定 HTML 对象可以监听哪些事件?

c# - 如何使用动态创建的 jquery 获取文本框的值?

javascript - Am Charts Javascript 版本,未在类别轴上显示完整的标签数

jquery - JqG​​rid需要超链接-需要通过Jquery捕获值

c# - ASP.NET Web 项目模板包含 2 个用于导入 Microsoft.WebApplication.targets $VSToolsPath) 与 $(MSBuildExtensionsPath32) 的条目

来自 php 的 Javascript 和 html 带空格

javascript - 如何用Javascript让Javascript运行在页面的最后一个进程

asp.net - NInject 可以在中等信任托管中工作吗?

javascript - DOM 更改后,单击时 jQuery 不起作用

javascript - If 与 while 在特定的 JavaScript 代码中