我需要一些 JavaScript 逻辑方面的帮助。我有这个隐藏/显示的代码。如果满足任何一个或条件,我需要它才能工作。目前它正在满足前两个条件。我对 javascript 相当陌生,所以不确定我是否正确编写了条件。
JavaScript:
$("input[name=JECT]").click(function() {
if ((this.value == "no" || this.value == "JECTindividual_electronic") || document.getElementById("JECTRenewal")== "yes")
$('#JECTvolumeshow').slideUp();
else $('#JECTvolumeshow').slideDown();
});
这是 html:
<h2>Subscription Options</h2>
<h3>Please choose the options you desire.</h3>
<div id="JECTshow" style="display:none">
<p style="text-indent:30px;">Length of subscription:</br>
<label><input type="radio" name="JECTsub" value="1" checked <?php if(@$JECTsub=="1"){ echo "checked";}?>> 1 year</label></br>
<label><input type="radio" name="JECTsub" value="2" <?php if(@$JECTsub=="2"){ echo "checked";}?>> 2 years</label></br>
<label><input type="radio" name="JECTsub" value="3" <?php if(@$JECTsub=="3"){ echo "checked";}?>> 3 years</label></br>
<label><input type='checkbox' name='JECTrenewal' value='yes' <?php if(@$JECTrenewal=="yes"){ echo "checked";}?>> Check box if this is a renewal</label></p></br>
<div id="JECTvolumeshow" style="display:none">
<p style="text-indent:30px;">I would like my subscription to start with:</br></p>
<label><input type="radio" name="JECTvolume" value="current" checked<?php if(@$JECTvolume=="current"){ echo "checked";}?>><?php echo "Volume ".$JECTsubscribeVolume['vol'].", Year ".$JECTsubscribeVolume['year']?></label></br>
<label><input type="radio" name="JECTvolume" value="next"<?php if(@$JECTvolume=="next"){ echo "checked";}?>><?php echo "Volume ".++$JECTsubscribeVolume['vol'].", Year ".++$JECTsubscribeVolume['year']?></label></p>
</div>
</div>
最佳答案
document.getElementById("JECTRenewal")
返回 DOM 节点,而不是元素的任何单个属性。因此,您不会直接将其与“yes”
进行比较。要查看该复选框是否已选中,请将 JECTrenewal
设置为元素的 id 并查找 checked
属性:
if ((this.value == "no" || this.value == "JECTindividual_electronic")
|| document.getElementById("JECTrenewal").checked)
...
或者,使用 jQuery:
if ((this.value == "no" || this.value == "JECTindividual_electronic")
|| $("input[name=JECTrenewal]").is(":checked"))
...
关于Javascript条件隐藏/显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37330706/