如何在类中查找CheckBox并使用JQuery隐藏CheckBox?

时间:2014-01-31 07:19:18

标签: jquery checkbox show-hide

我有一个Grid view,其中第一列是 CheckBox 。现在,我想要隐藏所选行的CheckBox。 我已尝试使用以下内容隐藏CheckBox,但它不起作用。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true"
    PageSize="100" AllowSorting="true" DataSourceID="sqlUsers" DataKeyNames="ttppcid"
    Width="100%" OnRowCommand="GridView1_RowCommand" EmptyDataText="No Data Found"
    OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" Visible="true">
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" Height="30" class="mychk" rowid="<%# Container.DataItemIndex+1 %>" />
            </ItemTemplate>
        </asp:TemplateField>
//some more templates here
        <asp:TemplateField HeaderText="" ItemStyle-Width="70px" ItemStyle-HorizontalAlign="Center"
            HeaderStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Button ID="BtnSource" runat="server" Text="Source" rowid="<%# Container.DataItemIndex+1 %>"
                    class="showButton" OnClick='<%# "return SetRowValues("+Eval("ttppcid")+",this.id,"+Eval("Fair")+","+Eval("Good")+","+Eval("Mint")+","+Eval("Poor")+","+Eval("Fair")+")"%>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

使用Javascript:

function SetRowValues(id, controlid, fair, good, mint, nnew, poor, broken) {
    var rowid = $("#" + controlid).attr("rowid");
    var chkBoxID;
    var chkRowid;
    $('.mychk').css("display", "block");

    $('.mychk').each(function() {

        chkBoxID = this.id;
        alert(chkBoxID);
        chkRowid = $("#" + chkBoxID).attr("rowid");
        alert(chkBoxID + " ROW :" + chkRowid);


        if (chkRowid == rowid) {
            $("#" + chkBoxID).css("display", "none");
        }
        else {
            $("#" + chkBoxID).css("display", "block");

        }
    });
return false;
}

另外还有:

<script type="text/javascript">
    $(document).ready(function() {
    $(function() {
        $(".showButton").on("click", function() {
            alert(".showButton clicked");
            $(this).closest("tr").find(":checkbox").hide();
        });
    });
    });
</script>

呈现标记:

渲染CheckBox:

<td align="center" style="width: 30px; display: block;">
   <span class="mychk" rowid="1" style="display: block; height: 30px;"><input id="ctl00_ContentPlaceHolder1_GridView1_ctl02_chkSelect" type="checkbox" name="ctl00$ContentPlaceHolder1$GridView1$ctl02$chkSelect"></span>
</td>

**Rendered Button in Grid:**
<td align="center" style="width: 70px; display: block;">
 <input type="submit" name="ctl00$ContentPlaceHolder1$GridView1$ctl03$BtnSource" value="Source" onclick="return SetRowValues(6,this.id,222.0000,222.0000,222.0000,222.0000,222.0000);WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$GridView1$ctl03$BtnSource&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_ContentPlaceHolder1_GridView1_ctl03_BtnSource" class="showButton" rowid="1" style="">
</td>

注意:我想隐藏CheckBox,其中Button的'rowid'与CheckBox的'rowid'匹配。

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

 function SetRowValues(id, controlid, fair, good, mint, nnew, poor, broken) {
    var rowid = $("#" + controlid).attr("rowid");
    var chkBoxID;
    var chkRowid;
    $('span.mychk').each(function() {
     chkBoxID = $(this).attr('id');
      chkRowid = $(this).attr('rowid');
      if (chkRowid == rowid) {
           $(this).hide();
           $(this).closest("td").css("border","none");
      }
      else {
           $(this).show();
           $(this).closest("td").css("border", "1px solid grey");
       }
    });
    return false;
}