【问题标题】:How to Hide CheckBox of Selected Row from GridView using Javascript?如何使用 Javascript 从 GridView 隐藏选定行的复选框?
【发布时间】:2014-02-22 20:20:32
【问题描述】:

我有一个GridView,第一列有CheckBox,第二列有Button。当用户点击Button 时,current rowCheckBox 必须是Invisible,使用Javascript

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

alert 中一切正常,我得到了空。无法获取 CheckBox 控件的 ID

也试过了:

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

网格中的渲染按钮:

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

有什么想法吗?

帮助赞赏!

【问题讨论】:

  • 运行应用程序后最终的 html 是什么?你能粘贴html输出吗?
  • 如果我理解正确,您无法使用 javascript 实现这一点。 asp 按钮将进行回发,隐藏的复选框将再次可见。如果您不想进行回发,请使用 html-button。
  • @Rahat 请检查编辑..!
  • 嗨@Esa,它们不是回发,因为我的返回语句总是返回“假”。一切正常,因为我只需要无法隐藏复选框。
  • 您使用的是服务器端的 OnClick-property,您是指 OnClientClick 吗?无论如何,如果您不想回发,请首先不要使用 asp-button。

标签: javascript jquery gridview checkbox


【解决方案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;
}

谢谢大家..! :)

【讨论】:

  • 很高兴你找到了解决方案,但好像 var rowid = $("#" + controlid).attr("rowid");有了这个,它在你以前的代码中没有控制的 id.. 现在它是如何开始工作的?
  • 嘿@SagarS.Dhanorkar,所有代码都相同,唯一的变化是我为 .mychk 复选框选择属性的方式错误。所以,它不会起作用。你有没有注意到在上一篇文章中我使用了“this.id”,这是错误的,它必须是 $(this).attr('id')。但是在选择属性后,我也得到了行 id..它工作了......! :D
【解决方案2】:

试试下面的:

var result = $('#<%=GridView1.ClientID %> tr td input[id*="chkSelect"][type=checkbox]:checked').map(function () {

    return $(this).closest('tr').find('td').eq(2).text();

    }).get().join();

【讨论】:

  • 嗨@Sagar S.Dhanorkar,我已将其粘贴到 SetRowValues() javascript 方法中。但它不会工作..!我做错了什么请告诉我..!
  • 你能解释一下我不理解的代码吗.get()、.map 和 join() 这些对我来说是新的..!所以..?
【解决方案3】:

首先要在不回发的情况下执行此操作,请将 asp-button 更改为,输入 type=button 或 html-button:

<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>
                <input type="button" class="showButton" value="Source"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后使用这样的脚本来隐藏复选框。您不需要复选框的 id,只需找到父行并隐藏复选框即可:

$(function() {
    $(".showButton").on("click", function() {
          $(this).closest("tr").find(":checkbox").hide();
    });
});

【讨论】:

  • 为什么不呢?你想隐藏复选框吗?
猜你喜欢
  • 1970-01-01
  • 2013-03-01
  • 2013-08-10
  • 2016-05-03
  • 1970-01-01
  • 2019-02-15
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多