2011-05-26 37 views
2

我想有一個輸入複選框觸發問題的B版本是否出現。如果他們選擇永遠不會消失。我使用兩個班來聽。點擊.showb時,找到下一個.bques並顯示它。單擊.hidb時,請查找下一個.bques並隱藏它。看起來很容易,但我抨擊我爲什麼它不工作。請幫忙。jquery下一個,這不工作

使用$(「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> 

回答

3

更改爲

$().ready(function() { 

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

}); 
+0

+1這裏是JSfiddle http://jsfiddle.net/roXon/RGAHF/ – 2011-05-26 17:04:43

+0

謝謝謝謝! – McPace 2011-05-26 19:32:32

0

是的,這是一個孩子,兄弟姐妹的問題。問題是.showb一旦到達其父項p元素的末尾就會停止。所以它會查看跟隨對象 - <label>,<br>,<input>的某些組合,直到它到達</p>,此時它會停止搜索並執行show()

你想要的東西像

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

感謝您的額外解釋。 – McPace 2011-05-26 19:39:26

1

$(this)工作,這是你的next這是不正確的使用。

jQuery docs

的.next([選擇器])

獲取緊接在該組匹配 元件的同胞的各元素的 。如果提供了一個選擇器, 只有在 與該選擇器匹配時纔會檢索下一個兄弟。

沒有與您的選擇器匹配的.hidb無線電的兄弟姐妹。你需要的是父母<p>元素的下一個兄弟。

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

.parent([選擇器])

獲取的當前 匹配元素中的每個元素的父, 任選被選擇器過濾。

調試這樣的問題的第一個步驟通常是

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

這表明你,你的選擇是不匹配的任何元素。