2

質問のbバージョンが表示されるかどうかを入力チェックボックスでトリガーしようとしています。彼らが決して選択しない場合、それは消えます。私は聞くために2つのクラスを使用しています。.showb をクリックすると、次の .bques を見つけて表示します。.hidb をクリックしたら、次の .bques を見つけて非表示にします。かなり簡単に思えますが、なぜ機能しないのか頭を悩ませています。助けてください。

$("p").next(".bques").show("fast"); の使用 $(this).next(".bques").show("fast");の代わりに ページ上のすべての b の質問を表示します。私も.nextAllを試してみましたが、うまくいきませんでした。なぜこれが機能しないのですか?兄弟姉妹の問題でしょうか?

コード:

<!DOCTYPE html>
<html>
<head>
<style>
div { background:#def3ca; margin:3px; width:80px; 
display:none; float:left; text-align:center; }.bques{display:none;}
</style>
<script src="jquery-1.4.2.min.js"></script>
<script>$().ready(function() {

$(".showb").click(function () { $(this).next(".bques").show("fast"); });
$(".hidb").click(function () { $(this).next(".bques").hide("fast"); });

});
</script>
</head>
<body>
<p><strong>8a.</strong> How frequently do you see someone take a shortcut that could be dangerous for patients (for example, not washing hands long enough, not changing gloves when appropriate, failing to check armbands, forgetting to perform a safety check)? <br />
<input type="radio" name="shortcut" value="1" id="never8"  tabindex="38" class="required hidb" title="Please choose one of these answers." /><label for="never8"> Never</label> <br />
<input type="radio" name="shortcut" value="2" id="once8"  tabindex="39" class="showb" /><label for="once8"> Once a year</label> <br />
<input type="radio" name="shortcut" value="3" id="twice8"   tabindex="40" class="showb" /><label for="twice8"> Twice a year</label>
</p>

<p class="bques"><strong>8b.</strong> Think of the most recent times you've seen this happen. Who have you spoken with about the problem. Check each box that applies: <br />
<input type="checkbox" name="shortcut_cc[]" value="none" id="none8"   tabindex="44" title="Please choose at least one of these answers." /><label for="none8"> Have not spoken with anyone.</label> <br />
<input type="checkbox" name="shortcut_cc[]" value="ff"  id="ff8"  tabindex="45" /><label for="ff8"> Have spoken with friends and family.</label> <br />
<input type="checkbox" name="shortcut_cc[]" value="coworkers" id="coworkers8" tabindex="46" /><label for="coworkers8"> Have spoken with some of my co-workers.</label> <br />
</p>

<p><strong>9a.</strong>  How frequently do you see a situation where someone might be making a mistake when doing an assessment, doing triage, diagnosing, suggesting treatment/medication options, or performing a procedure? <br />
<input type="radio" name="mistake" value="1" id="never9"   tabindex="51" class="required hidb" title="Please choose one of these answers." /><label for="never9"> Never</label> <br />
<input type="radio" name="mistake" value="2" id="once9"   tabindex="52" class="showb" /><label for="once9"> Once a year</label> <br />
<input type="radio" name="mistake" value="3" id="twice9"   tabindex="53" class="showb" /><label for="twice9"> Twice a year</label> <br />
</p>

<p class="bques">9b. Think of the most recent times you've seen this happen. Who have you spoken with about the problem. Check each box that applies: <br />
<input type="checkbox" name="mistake_cc[]" value="none" id="none9"   tabindex="57" title="Please choose at least one of these answers." /><label for="none9"> Have not spoken with anyone.</label> <br />
<input type="checkbox" name="mistake_cc[]" value="ff"  id="ff9"  tabindex="58" /><label for="ff9"> Have spoken with friends and family.</label> <br />
<input type="checkbox" name="mistake_cc[]" value="coworkers" id="coworkers9" tabindex="59" /><label for="coworkers9"> Have spoken with some of my co-workers.</label>
</p>

</body>
</html>
4

3 に答える 3

3

への変更

 $().ready(function() {

  $(".showb").click(function () { $(this).parent().next(".bques").show("fast"); });
  $(".hidb").click(function () { $(this).parent().next(".bques").hide("fast"); });

 });
于 2011-05-26T16:57:53.480 に答える
1

$(this)は機能していますが、それはあなたの使用法nextが正しくありません。

jQueryドキュメントから:

.next([セレクター])

一致した要素のセット内の各要素の直後の兄弟を取得します。セレクターが提供されている場合、そのセレクターと一致する場合にのみ、次の兄弟を取得します。

.hidbセレクターに一致するラジオの兄弟はありません。必要なのは、 <p>要素の次の兄弟です。

$(this).parent().next(".bques").hide("fast");

.parent([セレクター])

一致した要素の現在のセット内の各要素の親を取得します。オプションでセレクターによってフィルター処理されます。

このような問題をデバッグする最初のステップは、通常、

alert($(this).next(".bques").length);

これは、セレクターがどの要素とも一致していないことを示しています。

于 2011-05-26T17:00:17.897 に答える
0

はい、それは子供兄弟の問題です。問題は、親要素.showbにあるものの最後に到達すると停止することです。pしたがって、followオブジェクト(との組み合わせ)を調べ、に到達する<label>まで、検索を停止して何も実行しません。<br><input></p>show()

あなたは次のようなものが欲しい

$(".showb").click(function () { $(this).parent().next(".bques").show("fast"); });
$(".hidb").click(function () { $(this).parent().next(".bques").hide("fast"); });
于 2011-05-26T16:58:16.057 に答える