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

私が目にする問題の 1 つは、変数 mgift が関数 showHide で定義されているが、変更イベント ハンドラーをその外部に割り当てようとしているということです。宣言を var shcompany の後に置きます。

また、ページにレンダリングされる実際の HTML を把握し、正しいセレクターを使用して必要な要素を取得します。

于 2013-03-08T18:25:35.497 に答える
0

選択したラジオボタンを取得するには、次のようなことをしたいと思います

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