【问题标题】:How to Find a CheckBox inside a Class and Hide the CheckBox using JQuery?如何在类中查找 CheckBox 并使用 JQuery 隐藏 CheckBox?
【发布时间】:2014-01-31 16:58:48
【问题描述】:

我有一个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>

渲染标记:

渲染的复选框:

<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>

注意:我想隐藏 Button 的“rowid”与 CheckBox 的“rowid”匹配的 CheckBox。

【问题讨论】:

标签: jquery checkbox show-hide


【解决方案1】:

这是我的解决方案:

 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多