[英]How can I show/hide one <div> when a radio button is clicked on, when all the <div>s have the same class?
I have this JavaScript code: 我有以下JavaScript代码:
$("#midden-offertes form .subvragen").hide();
$("#midden-offertes form .veld .radio :radio").change(function()
{
if (this.value === 'Ja')
{
$("#midden-offertes form .subvragen",this).show()
}
else
{
$("#midden-offertes form .subvragen",this).hide();
}
});
I also have a <form>
with different radio buttons. 我也有一个带有不同单选按钮的
<form>
。 When I click on a radio button, the .subvragen
<div>
must be shown. 当我单击单选按钮时,必须显示
.subvragen
<div>
。
But every .radio
button has its own .subvragen
<div>
. 但是每个
.radio
按钮都有其自己的.subvragen
<div>
。 How can I change this JavaScript code so that only the .subvragen
within each radio button is shown when I click on that radio button? 如何更改此JavaScript代码,以便在单击该单选按钮时仅显示每个单选按钮中的
.subvragen
?
When I click on a radio button now, all the .subvragen
<div>
s are shown. 现在单击单选按钮时,将显示所有
.subvragen
<div>
。
Edit: 编辑:
I have uploaded the files. 我已经上传了文件。 You can see them here: http://mikevierwind.nl/karel/offerterelatie.html
您可以在这里看到它们: http : //mikevierwind.nl/karel/offerterelatie.html
Looking at the HTML you've got , you could use the following JavaScript (I think): 查看您拥有的HTML ,可以使用以下JavaScript(我认为):
$("#midden-offertes form .veld .radio :radio").change(function()
{
var $subvragen = $(this).parents('.veld').siblings('.subvragen');
if (this.value === 'Ja')
{
$subvragen.show();
}
else
{
$subvragen.hide();
}
});
Alternatively, if you could add id
attributes to your subvragen
<div>
s and radios, you could use them — it'd make the JavaScript less reliant on the HTML nesting. 另外,如果您可以将
id
属性添加到subvragen
<div>
和收音机,则可以使用它们-这将使JavaScript减少对HTML嵌套的依赖。
Eg if your HTML were like this: 例如,如果您的HTML是这样的:
<div id="midden-offertes">
<form>
<div class="veld">
<div class="radio">
<input name="geoff" id="geoff_1" value="a" />
</div>
</div>
<div class="subvragen" id="subvragen_geoff_1"></div>
<div class="veld">
<div class="radio">
<input name="geoff" id="geoff_2" value="b" />
</div>
</div>
<div class="subvragen" id="subvragen_geoff_2"></div>
<div class="veld">
<div class="radio">
<input name="geoff" id="geoff_3" value="c" />
</div>
</div>
<div class="subvragen" id="subvragen_geoff_3"></div>
</form>
</div>
Then your JavaScript would go something like this: 然后,您的JavaScript将如下所示:
$("#midden-offertes form .subvragen").hide();
$("#midden-offertes form .veld .radio :radio").change(function()
{
if (this.value === 'Ja')
{
$(".subvragen#subvragen_" + this.id).show()
}
else
{
$(".subvragen#subvragen_" + this.id).hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.