繁体   English   中英

隐藏基于所选单选按钮的节目

[英]Hide show based on selected Radio Button

我正在尝试一些JS,以开发自己正在使用的表单。 目的是隐藏用户是否尚未选择“是”。 这就是我所坚持的地方:HTML:

<asp:RadioButtonList runat="server" ID="rbMatchingGift" ClientIDMode="Static">
    <asp:ListItem Value="0" Text="No"></asp:ListItem>
    <asp:ListItem Value="1" Text="Yes"></asp:ListItem>
</asp:RadioButtonList>
<li id="shcompany">                 
    <label for="txtCompanyName">Company Name</label>
    <asp:TextBox runat="server" ID="txtCompanyName" CssClass="narrow" />   
    <label for="txtCompanyPhone">Company Phone Number</label>
    <asp:TextBox runat="server" ID="txtCompanyPhone" CssClass="narrow" />       
</li> 

JS:

$(document).ready(function () { 
    var shcompany = $('#shcompany'); 
    showHide();
    mgift.change(function () {
        showHide();
    });

    function showHide() {
        var mgift = $('#rbMatchingGift');
        var shcompany = $('#shcompany');

        if (mgift.val() == "1") {
            shcompany.show();
        } else {       
            shcompany.hide();        
        }
    }
});

在此处输入图片说明

<asp:RadioButtonList runat="server" ID="rbMatchingGift" ClientIDMode="Static">
    <asp:ListItem Value="0" Text="No"></asp:ListItem>
    <asp:ListItem Value="1" Text="Yes"></asp:ListItem>
</asp:RadioButtonList>
<li id="shcompany">
    <label for="txtCompanyName">Company Name</label>
    <asp:TextBox runat="server" ID="txtCompanyName" CssClass="narrow" />
    <label for="txtCompanyPhone">Company Phone Number</label>
    <asp:TextBox runat="server" ID="txtCompanyPhone" CssClass="narrow" />
</li>
<script>

    $(document).ready(function () {
        showHide();
        $('#rbMatchingGift input').change(function () {
            showHide();
        });

        function showHide() {
            var mgift = $('#rbMatchingGift input:checked');
            var shcompany = $('#shcompany');

            if (mgift.val() == "1") {
                shcompany.show();
            } else {
                shcompany.hide();
            }
        }
    });
</script>

我认为要选择选中的单选按钮,您需要执行以下操作

var rblSelectedValue = $("#<%= rbMatchingGift.ClientID %> input:checked"); 

我看到的问题之一是变量mgift是在showHide函数中定义的,但是您正在尝试在其外部分配更改事件处理程序。 将声明放在var shcompany之后。

另外,找出您在页面上呈现的实际HTML,并使用正确的选择器来获取所需的元素。

暂无
暂无

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

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