2012-08-09 33 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。