问题描述
|
我试图让一个输入复选框触发问题的b版本是否出现。如果他们选择永不消失,它就会消失。我正在使用两个类进行监听。单击.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>
解决方法
改成
$().ready(function() {
$(\".showb\").click(function () { $(this).parent().next(\".bques\").show(\"fast\"); });
$(\".hidb\").click(function () { $(this).parent().next(\".bques\").hide(\"fast\"); });
});
,$(this)
正在工作,这是您对next
的使用,这是不正确的。
从jQuery文档中:
.next([选择器])
获取紧随其后的同级
匹配集中每个元素的
元素。如果提供了选择器,
仅在以下情况下检索下一个同级
它与该选择器匹配。
selector4ѭ收音机中没有与您的选择器匹配的兄弟姐妹。您需要的是父“ 5”元素的下一个同级。
$(this).parent().next(\".bques\").hide(\"fast\");
.parent([选择器])
获取元素中每个元素的父元素
当前的一组匹配元素,
(可选)由选择器过滤。
调试此类问题的第一步通常是
alert($(this).next(\".bques\").length);
这表明您的选择器不匹配任何元素。
,是的,这是一个孩子问题。问题在于,一旦.showb
到达其父ѭ9what元素的末尾,它就会停止。因此它将查看跟随对象-<label>
,<br>
和<input>
的某种组合,直到达到</p>
,这时它将停止搜索并在没有任何内容的情况下执行show()
。
你想要类似的东西
$(\".showb\").click(function () { $(this).parent().next(\".bques\").show(\"fast\"); });
$(\".hidb\").click(function () { $(this).parent().next(\".bques\").hide(\"fast\"); });