根据选定的单选按钮隐藏节目

时间:2013-03-08 18:16:36

标签: javascript jquery asp.net html5

我正在尝试使用一些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();        
        }
    }
});

3 个答案:

答案 0 :(得分:1)

enter image description here

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

答案 1 :(得分:0)

我想要获得所选的单选按钮,你想要做这样的事情

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

答案 2 :(得分:0)

我看到的一个问题是变量mgift是在showHide函数中定义的,但是你试图在它之外分配一个change事件处理程序。在var shcompany之后放置声明。

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