Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

asp.net mvc 2 - JQuery checkbox manipulation within a foreach loop

I have made a table as below.

<div class="grid_top_button">
        <div class="left_top_curve">
                &nbsp;</div>
        <div class="right_top_curve">
                &nbsp;</div>
        <input name="input" type="button" id="addSelected" name="addSelected" class="section_btn"
                value="Link" />
</div>
<table id="LstDocTemp" border="0" cellpadding="0" cellspacing="0" class="instruction_grid">
    <tr>
        <th align="left" class="ins_sl_no">
                        Sl No.
                </th>
                <th align="left" class="selct_column">
                        <input type="checkbox" id="chkSelectAll" name="chkSelectAll" />
                </th>
                <th align="left" class="doc_title_1">
                        Document title
                </th>
                <th align="left" class="description">
                        Description
                </th>
                <th align="center" class="revision">
                        Revision
                </th>
                <th align="left" class="part_no">
                        Parts name
                </th>
                <th align="center" class="issue_no">
                        Issue
                </th>
                <th align="center">
                        Link
                </th>
        </tr>
        <% int slNo = 1; %>
        <%foreach (var item in Model)
            { %>
        <tr id="<%= Html.Encode(item.DocId) %>">
                <td>
                        <%= slNo %>
                </td>
                <td>
                        <input type="checkbox" name="chkItem" class="chk" id="chkbox_<%=Html.Encode(item.DocId) %>" />
                </td>
                <td>
                        <%= Html.Hidden("DocTitle", item.DocTitle)%>
                        <a href='<%= Url.Action("DetailsDocumentTemplate", "Document", new { id = item.DocId })%>'>
                                <%=Html.Encode(item.DocTitle) %></a>
                </td>
                <td>
                        <%= Html.Hidden("DocDesc", item.DocDesc)%>
                        <%= Html.Encode(item.DocDesc) %>
                </td>
                <td class="dark_highlight">
                        <%= Html.Hidden("DocRevision", item.DocRevision)%>
                        <%= Html.Encode(item.DocRevision) %>
                </td>
                <td>
                        <%= Html.Hidden("PartListId", item.PartListId)%>
                        <%= Html.Hidden("PartNo", item.PartNo)%>
                        <%= Html.Encode(item.PartNo) %>
                </td>
                <td class="light_highlight">
                        <%= Html.Hidden("IssueNo", item.IssueNo)%>
                        <%=Html.Encode(item.IssueNo) %>
                </td>
                <td>
                        <%= Html.Hidden("DocId", item.DocId)%>
                        <a class="icon_add" title="Add">Add</a>
                </td>
        </tr>
        <%slNo++;
            } %>
</table>

I need to achieve the follwing:

  1. Perform action in the controller for the rows which are selected by checking the checkbox (name="chkItem").
  2. Check/uncheck the checkboes (name="chkItem") when the checkbox (name="chkSelectAll) is checked.
  3. After selecting all by checking the chkSelectAll checkbox, and unchecking anyone should uncheck the chkSelectAll checkbox.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

for first point you can do this...

if($('.chk').is(':checked')){
 //perform the action in controller
}

for second point -

$(".selectAll").click(function(){
  var checked = $("#selectall").attr("checked");
  $(".chk").attr("checked",checked);
}

for third point -

$(".chk").click(function(){
 var checkedBoxes= $(".selectone").map(function(){ return jQuery(this).attr("checked");}).get();
 var flg = true;
 if(jQuery.inArray(false, net)){
   flg= false;
 }
 $("#selectall").attr("checked",flg);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...