1

テキスト入力の横にたくさんのラジオ ボタンがあります。ラジオ ボタンの状態が変更されると、「.IdPassportRadioButton」のクラスを持つすべてのラジオ ボタンを取得するメソッドを実行します。ラジオ ボタンごとに、次のテキスト入力を見つけて、ラジオ ボタンに応じてその入力を有効または無効にしたいと考えています。

HTML は次のようになります。

<div class="editor-container" style="">
        <div class="editor-label">
            <%=Html.RadioButtonFor(m => m.IsPassport, false, 
                                        new { @class = "IdPassportRadioButton" })%>
            ID Number
        </div>
        <div class="editor-field">
            <%=Html.TextBoxFor(m => m.IdentityNumber, 
                                   new { @class = "textbox IdPassportTextBox"})%>
        </div>
    </div>

JQuery メソッド:

$(".IdPassportRadioButton").each(function() {
        if ($(this).is(":checked")) {
            $(this).next(".IdPassportTextBox").removeAttr("disabled");
        } else {
            $(this).next(".IdPassportTextBox").attr("disabled", "disabled");
            }
    });

ご覧のとおり、次の TextBox をクラス ".IdPassportTextBox" で検索しようとしましたが、うまくいきません。次のテキストボックスを取得する行を除いて、すべてが機能します。

助言がありますか

4

2 に答える 2

1

jQueryの「.Parent()」メソッドを使用して、囲んでいる「editor-container」を取得し、その中から「IdPassportTextbox」を選択します。

于 2012-11-30T06:05:26.827 に答える
1

セレクターでclosest()を使用してクラス IdPassportTextBox"editor-container"に到達し、セレクターメソッドでfind()を使用してテキストボックスにアクセスできます。parent of TextBox".IdPassportTextBox"

$(".IdPassportRadioButton").each(function() {
    if ($(this).is(":checked")) {
        $(this).closest(".editor-container").find(".IdPassportTextBox").removeAttr("disabled");
    } else {
        $(this).closest(".editor-container").find(".IdPassportTextBox").attr("disabled", "disabled");
        }
});
于 2012-11-30T05:47:57.683 に答える