これは、SOに関する私の最初の詳細な質問です。
こんにちは、フォームがあり、ユーザーがドロップダウン リストで [その他] オプションを選択したときに、ドロップダウン リストの後にテキスト フィールドを表示したい場合がたくさんあります。
標準の命名規則を使用していますが、ドキュメント内の DDL/テキスト フィールドのペアと同じ数の関数を使用する必要がありますか?それとも、DDL のクラスで呼び出すことができる単一の関数を使用できますか? HTMLは次のとおりです。
<label for="spotter">Spotter:</label>
<select id="spotter" required>
<option value="Snoop Dogg">Snoop Dogg</option>
<option value="MC Escher">MC Escher</option>
<option value="Linus Thorvalds">Linus Thorvalds</option>
<option value="Other">Other</option>
</select>
<br/>
<div id="spotter_other_div"><label for="spotter_other">Other Spotter:</label><br><input type="text" name="spotter_other" id="spotter_other" size="50" required/></div>
そしてここにjQuery/javascriptがあります:
$(document).ready(function(){
$("#spotter").change(function () {
//alert("You just changed the value of the #spotter drop down menu.");
if ($(this).val() == "Other" )
//alert("You just changed the value of the #spotter drop down menu to 'Other.'");
$("#spotter_other_div").css("visibility", "visible");
else
$("#spotter_other_div").css("visibility", "collapse");
});
});
テキストフィールドを含む div の初期状態は、css で「折りたたみ」です。
私はjQueryを学んでいて、一般的なケースで何かをする方法を知っているところです.これが私が書くことができる関数なのか、それとも明示的に行う必要があるのか.
このページは進行中の作業であるため、任意の提案を歓迎します (たとえば、div の代わりにスパンを使用してテキスト フィールドを含めるなど)。
ありがとう!