问题:
var selected = $("#tbList").datagrid("getSelections");
selected的选中项 会包含上次已删掉的选中项
例子:
比如list中有ArticleID的值1、2、3,
删除1后,选中2、3继续删除,则selected包含了值1,导致出错。
解决方法:在删除数据成功后,使用datagrid("clearSelections")清空所有的已选择项
代码:
@using XStudio.XWebFramework.Extensions <script type="text/javascript" defer="defer"> var List = {}; List.DeleteSelected = function (e) { var selected = $("#tbList").datagrid("getSelections"); if (selected.length == 0) { alert("请选择要删除的文章?"); return; } var idString = ""; $.each(selected, function (index, item) { idString += item.ArticleID + ","; }); if (!confirm("确认要删除选中文章信息?")) return; $.post($(e).attr("href"), { id: idString }, function (data) { if (data.IsOK) { $("#tbList").datagrid("clearSelections");//解决方法:在删除数据成功后清空所有的已选择项 $('#tbList').datagrid('reload'); } else alert(data.Description); }); } $(document).ready(function () { $('#tbList').datagrid({ pagination: true }); }); </script><div class="tt-attach"> <ul class="op"> @Html.WebPartButton("删除选中", "Delete", null, new { onclick = "List.DeleteSelected(this); return false;", @class = "btn-remove" }) </ul> </div> <div style="padding: 5px 20px 5px 10px;"> <table id="tbList" striped="true" rownumbers="true" fix="true" fitColumns="true" title="" idfield="ArticleID" checkbox="true" url="@Url.Action("ListData")"> <thead> <tr> <th field="ArticleID" checkbox="true" width="30"> </th> </tr> </thead> </table> </div>