如何从数据角色按钮弹出警报?

时间:2012-08-09 14:33:19

标签: asp.net-mvc razor

我想在用户点击删除按钮时显示警告,如下所示:

@Html.ActionLink("Delete", "Delete", new { id = Model.ID }, new { @data_role = "button" })

我不确定如何通过此按钮获取onclick的id和事件。

1 个答案:

答案 0 :(得分:1)

你可以给这个锚一个id:

@Html.ActionLink(
    "Delete", 
    "Delete", 
    new { id = Model.ID }, 
    new { id = "delete", data_role = "button", data_id = Model.ID }
)

然后使用jQuery订阅click事件:

$(function() {
    $('#delete').click(function() {
        var id = $(this).data('id');
        return confirm('Are you sure you want to delete record with id: ' + id);
    });
});

如果您不使用jQuery但是使用简单的javascript:

window.onload = function() {
    document.getElementById('delete').onclick = function() {
        var id = this.getAttribute('data-id');
        return confirm('Are you sure you want to delete record with id: ' + id);
    };
};

.confirm() javascript函数显示消息,并根据用户是否单击“确定”或“取消”按钮返回true或false。