简体   繁体   English

html服务器控件复选框以asp.net Web形式由jQuery隐藏

[英]html server control Check box hiding by jQuery in asp.net web form

I have a simple page with three check box with the auto populate the data by jQuery and the fourth one has no data(empty). 我有一个简单的页面,其中包含三个复选框,其中复选框通过jQuery自动填充数据,而第四个复选框则没有data(empty)。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MasterPageWithHTMLInput.aspx.cs" Inherits="Test_WSMS_TV.MasterPageWithHTMLInput" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            // alert('akash');
            $(document).ready(function() {
                var arr = ["val1", "val2", "val3"];
                $('#check label').each(function(index) {
                    if (index < arr.length)
                        $(this).text(arr[index]);    
                });    
            });
        </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

        <div class="container" id="check">
            <input type="checkbox" id="CheckBox3" runat="server" />
            <label>
            </label>
            <input type="checkbox" id="CheckBox4" runat="server" />
            <label>
            </label>
            <input type="checkbox" id="CheckBox5" runat="server" />
            <label>
            </label>
            <input type="checkbox" id="CheckBox6" runat="server" />
            <label>
            </label>
        </div>

    </asp:Content>

I want to hide the Check box and label which has no data (fourth one) by jQuery. 我想通过jQuery隐藏没有数据的复选框和标签(第四位)。

<input type="checkbox" id="CheckBox6" runat="server"/>
        <label>
        </label>

how can i do it by jQuery? 我如何通过jQuery做到这一点?

Perhaps try one of the following 也许尝试以下方法之一

$("#CheckBox6").hide();

or 要么

$("#CheckBox6").toggle();

You can use :empty selector 您可以使用:empty选择器

Select all elements that have no children (including text nodes). 选择所有没有子元素的元素(包括文本节点)。

Script 脚本

$('#check label:empty').hide();
$('#check label:empty').prev(':checkbox').hide();

I have found an dynamic solution. 我找到了动态解决方案。 populate ID and hide the rest of the empty text box 填充ID并隐藏其余的空白文本框

<script type="text/javascript">
       // alert('akash');
        $(document).ready(function () {
            var arr = ["val1", "val2", "val3"];
            $('#check label').each(function (index) {
                if (index < arr.length)
                    $(this).text(arr[index]);
                else {
                    //                    $('#check label:empty').prev(':checkbox').hide();
                    var i = index + 3;
                    var id = "#CheckBox" + i;
                    alert(id);
                    $(id).hide();
                }
            });
         //   $("#CheckBox6").hide();
        });

    </script>

and in main place holder clientidmode="Static" in asp.net is required. 并且在asp.net中需要主占位符clientidmode =“ Static”。

<input type="checkbox" id="CheckBox6" runat="server" clientidmode="Static" />
        <label>
        </label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM