javascript - JQuery ASP 隐藏问题

标签 javascript jquery asp.net

根据复选框的结果,我想隐藏/显示隐藏表(从页面加载中隐藏),要求提供更多信息。

下面是 JQuery 和 ASP,有人可以指出我正确的方向并让我知道这种方法做错了什么吗?

ASP

<h3>Table Heading</h3>
    <asp:Table ID="tbl1" runat="server">
        <asp:TableHeaderRow>
            <asp:TableHeaderCell>
                <h4>One</h4>
            </asp:TableHeaderCell>
        </asp:TableHeaderRow>
                    <asp:TableRow>
            <asp:TableCell>
                <asp:CheckBox ID=""Yes" runat="server" Checked="false" Text="Yes" />
                <asp:CheckBox ID="No" Text="No" runat="server" Checked="false" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    <asp:Table ID="tbl2" runat="server" Visible="false">
        <asp:TableHeaderRow>
            <asp:TableHeaderCell>
                <h4>Additional information</h4>
            </asp:TableHeaderCell>
        </asp:TableHeaderRow>
        <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="AddInfo" runat="server" TextMode="MultiLine"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
    </asp:Table>

JQuery

<script type="text/javascript">
    function toggleTable()
    {
        $(function () {
            $("#Yes").click(function () {
                $("#tbl2").show();
            })
            $("#No").click(function () {
                $("#tbl2").hide();
        })
    }
</script>

更新 1 按照建议我删除了包装功能,但这仍然不起作用。

<script type="text/javascript">
        $(function () 
        {
            $("#Yes").click(function () {
                $("#tbl2").show();
        })
            $("No").click(function () {
                $("#tbl2").hide();
            })
        })
</script>

原始 HTML

<h3></h3>
    <table id="MainContent_tbl1">
<tr>
    <th>
                <h4></h4>
            </th>
</tr><tr>
    <td><input id="MainContent_Yes" type="radio" name="ctl00$MainContent$Yes" value="Yes" />
    <label for="MainContent_Yes">Yes</label><input id="MainContent_No" type="radio" name="ctl00$MainContent$No" value="No" />
    <label for="MainContent_No">No</label></td>
</tr>

最佳答案

当您使用visible=false时在服务器控件中,它已经做到了这一点 - 它在呈现时从页面中完全删除了该控件。

More about Visible property - If this property is false, the server control is not rendered. You should take this into account when organizing the layout of your page. If a container control is not rendered, any controls that it contains will not be rendered even if you set the Visible property of an individual control to true. - http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible(v=vs.100).aspx

所以我建议,如果您要从服务器端代码显示/隐藏,则设置属性 (CSS) style= display none 。如果直接使用visible属性并将其设置为false,则会将控件从页面中移除,从而无法在页面中显示。

例如tbl1.Attributes.Add("style", "display:none");

在客户端使用style=display:none"隐藏它而不是visible=false .

另外,使用.ClientID获取 jquery 选择器的准确渲染 ID。喜欢

$("#<%=tbl1.ClientID%>").show();

关于javascript - JQuery ASP 隐藏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109556/

相关文章:

javascript - Angular 计时器自定义倒计时变量

javascript - document.getElementsByName ("") 相当于 YUI3.0

javascript - JSON.stringify 缺少属性

javascript - Jquery CSS-嵌套<li> 4层深度调用父级

javascript - 当父 div 中的数据表表被隐藏时,数据表数据表工具不起作用

javascript - 单个数组 [0] 项返回未定义

jquery - 在 mvc4 中调用从 View 中删除帖子?

c# - MySqlCommand.ExecuteScalar() 不返回任何内容

asp.net - 具有许多参数的 Web API httpget

asp.net - iOS 和 ADFS 保护的网站