开发者

ASP.NET MVC - how to make users confirm the delete

He, I have this page where i have checkboxes next to every item in a table, and want to allow the user to select some of them and press my delete button. I just cant come up with the jquery for making the confirm window and submitting only if 'yes' is pushed - this is my page

<%Html.BeginForm(); %>
<%List<ShopLandCore.Model.SLGroup> groups = (List<ShopLandCore.Model.SLGroup>)Model; %>
<%Html.RenderPartial("AdminWorkHeader"); %>
<table width="100%" id="ListTable" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="5" class="heading">
            <input type="submit" name="closeall" value="Close selected" />
            <input type="submit" name="deleteall" value="Delete selected" />
        </td>
    </tr>
    <tr>
        <th width="20px">
        </th>
        <th>
            Name
        </th>
        <th>
            Description
        </th>
        <th width="150px">
            Created
        </th>
        <th width="150px">
            C开发者_StackOverflow中文版losed
        </th>
    </tr>
    <%foreach (ShopLandCore.Model.SLGroup g in groups)
      { %>
    <tr>
        <td>
            <%=Html.CheckBox(g.Id.ToString()) %>
        </td>
        <td>
            <%=g.Name %>
        </td>
        <td>
            <%=g.Description %>
        </td>
        <td>
            <%=g.Created %>
        </td>
        <td>
            <%=g.Closed %>
        </td>
    </tr>
    <%} %>
</table>
<%Html.EndForm(); %>

Please note that its only for the delete that it should confirm, and not necessarily for the close button.


Simply add the following to the head of your page:

<script type="text/javascript">
    $(document).ready(function(){
        $("input[name='deleteall']").click(function() {
            return confirm('Delete all selected elements?');
        });
    });
</script>


You should put a javascript onclick method on your button, to display a messagebox and then stop processing the click if the user chooses no.

You can modify your delete button code to:

<input type="submit" name="deleteall" value="Delete selected" onclick="return confirm('Are you sure you want to Delete?');"/>


Here's how to register a click event to your button unobtrusively using jQuery (without mixing html markup and script logic):

$(function() {
    $('input[name=deleteall]').click(function() {
        return confirm('Are you sure');
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜