0

フォームに多くの選択があります。

フォームのいくつかの選択にオプション「その他」を追加したいと思います。「その他」を選択すると、非表示の入力が表示されます。

$(document).ready(function(){

$(".SelectOther").append('<option value=-1>Other</option>');
$(".OtherDiv").hide();
    $(".SelectOther").change(function()
    {
        if($(".SelectOther option:selected").text() == "Other")
        {
            $(".OtherDiv").show("slide", { direction: "up" }, 1000);
        }
        else
        {
            $(".OtherDiv").hide("slide", { direction: "down" }, 1000);
        }
    });

});

このコードはまだテストしていませんが、非表示の「OtherDiv」がすべて開きます

すべての選択は、テーブル内の TD 内にあります。

「その他」を選択すると、その TD 内の非表示の入力のみが表示されるようにするにはどうすればよいですか?

4

1 に答える 1

1

解決 :

を使用し$(this)ます。このようにして、正しいコンテキストにいることがわかります。

$(document).ready(function(){

   $(".SelectOther").append('<option value=-1>Other</option>');
   $(".OtherDiv").hide();
   $(".SelectOther").change(function() {
     var otherDiv =  $(this).closest("tr").find(".OtherDiv"); // find the .otherdiv within the tr
     if($(this).find("option:selected").text() == "Other") //find the selected option
     {
           otherDiv.show("slide", { direction: "up" }, 1000);
     }
     else
     {
           otherDiv.hide("slide", { direction: "down" }, 1000);
     }
    });

});

編集:

toggleClass("classname")クラスを追加または削除するには、またはaddClass("classname")/を使用できますremoveClass("classname")

ここで使用されているものに関する詳細情報:

$(this)

find()

  • ドキュメント: http://api.jquery.com/find/
  • 機能 : セレクター、jQuery オブジェクト、または要素によってフィルター処理された、一致する要素の現在のセット内の各要素の子孫を取得します。
于 2013-07-04T00:47:25.683 に答える