javascript - 根据选定的单选按钮隐藏显示

标签 javascript jquery asp.net html

我正在为我一直在处理的表单尝试一些 JS。目的是在用户未选择"is"时隐藏。这就是我遇到的问题: 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();        
        }
    }
});

最佳答案

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>

关于javascript - 根据选定的单选按钮隐藏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15300638/

相关文章:

c# - ASP.NET MVC - 将多个变量传递给模型?

javascript - 不能在 img 标签内使用 base64 图像吗?

javascript - Angular 2+ 管道附加零和逗号

c# - 无法在 javascript 中获取 html 文本框的值

javascript - 使用 moment.js 在 HTML5 <time> 元素中插入日期

asp.net - 请求卡在 IIS 工作进程中的 RequestAcquireState 中

javascript - Saga Redux 不在 post 中发送参数

php - 我的php网站的Metro风格动态磁贴

javascript - Jquery:使用计数器显示折扣

c# - 仅重定向根文件夹 web.config 上的 default.aspx