1

我正在为我一直在研究的表单尝试一些 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();        
        }
    }
});
4

3 回答 3

1

在此处输入图像描述

<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>
于 2013-03-08T19:03:39.467 回答
0

我认为要获得选定的单选按钮,您需要做这样的事情

var rblSelectedValue = $("#<%= rbMatchingGift.ClientID %> input:checked"); 
于 2013-03-08T18:22:58.647 回答
0

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

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

于 2013-03-08T18:25:35.497 回答