要素のセットを繰り返すフォームがあります。
class を使用した選択が変更されたら、 classを使用して次のテキスト入力のみcredit_req_select
を表示/非表示にし、場合によってはクリアしたいと考えています。credit_line_text
以下の change() func はテキストフィールドを見つけていません$(this)
。
これらのテキストフィールドを取得する正しい方法は何ですか?
私のHTMLは
<p class="file_info">*Credit Required:
<select name='default_credit_requirements' id='default_credit_requirements' class="credit_req_select">
<option></option>
<option value='1' selected>Do not include</option>
<option value='2' >Include if able</option>
<option value='3' >Must include</option>
</select>
</p>
<p class="file_info">
<label>*Credit Line:
<input type="text" name="default_credit_line" id="default_credit_line" class="credit_line_text" value="" />
</label>
</p>
私の関数は次のようになります。ここでの答えと同じです$(this) を使用した Jquery next隣接セレクタ
$('.credit_req_select').change(function() {
if(this.value > 1) {
$(this).next(".credit_line_text").show();
console.log($(this).next(".credit_line_text").attr('id'));
} else {
$(this).next(".credit_line_text").val('');
$(this).next(".credit_line_text").hide();
}
});
私もこの答えを試しましたjquery next() 何も返さない
$('.credit_req_select').change(function() {
if(this.value > 1) {
$(this).nextUntil(".credit_line_text").show();
console.log($(this).nextUntil(".credit_line_text").attr('id'));
} else {
$(this).nextUntil(".credit_line_text").val('');
$(this).nextUntil(".credit_line_text").hide();
}
});
また、これをjQuery $(this).next() ごとに var に割り当てようとしましたが、期待どおりに機能しませんでした