0

当用户单击删除按钮时,我想显示一个警报,如下所示:

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

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

4

1 回答 1

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);
    };
};

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

于 2012-08-09T15:01:18.290 回答