2012-08-09 39 views
0

我想显示一个警告,当用户点击删除按钮,如下所示:如何从数据 - 角色按钮弹出警报?

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

我不知道我怎样才能从这个按钮获得的onclick的ID和事件。

回答

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。