js操作複選框

js操作複選框

JavaScript 代碼:

//複選框全選
        $(function () {
            $("#select_all").click(function () {
                $("input[name='select_item']").attr("checked", this.checked);
            });
 //刪除所選項
        function delete_select() {
            var chk_value = "";
            $('input[name="select_item"]:checked').each(function () {
                chk_value += $(this).val() + ",";
            });
            if (chk_value.length == 0) {
                alert('你還沒有選擇任何內容')
            } else {
                $.messager.confirm('提示', '您想要刪除嗎?', function (r) {
                    if (r) {
                        $.post("/TeacherList/DeleteSelected", { "id": chk_value }, function (data) {
                            if (data == "ok") {
                                $('input[name="select_item"]:checked').each(function () {
                                    $(this).parent().parent().remove();//移除被選中的行
                                });
                                $.messager.show({
                                    title: '提示',
                                    msg: '該記錄已刪除,下次頁面刷新時將爲您重新編號',
                                    timeout: 2500,
                                    showType: 'slide'
                                });
                            } else {
                                $.messager.alert("提示", "刪除失敗" + data, "info");
                            }
                        });
                    }
                });
            }
        } 

HTML 部分代碼:

 <input type="button" value="刪除所選項" onclick="delete_select()"/>
        }
        @if (ViewData["list"] != null)
        {
            <table class="bordered" width="100%">
                <tr>
                   <th><input type="checkbox" id="select_all" /></th>
                   <th>賬號</th>
                    <th>姓名</th>
                    <th>所屬院系</th>
                    <th>性別</th>
                    <th>教師類型</th>
                    <th>工作單位</th>
                    <th>操作</th>
                </tr>
                @foreach (TeacherInfo teacherInfo in (List<TeacherInfo>)ViewData["list"])
                {
                    <tr>
                        <td><input type="checkbox" name="select_item"  value="@teacherInfo.id"/></td>
                        <td>@teacherInfo.UserId</td>
                        <td>@teacherInfo.name</td>
                        <td>@teacherInfo.departmentName</td>
                        <td>@teacherInfo.Tsex</td> 
                 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章