我需要一些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>
答案 0 :(得分:3)
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"))
...
答案 1 :(得分:1)
document.getElementById将返回null。以下是您想要的代码吗?
$("input[name=JECT]").click(function() {
if (this.value == "no" || this.value == "JECTindividual_electronic" || document.getElementById("JECTRenewal") !== null)
$('#JECTvolumeshow').slideUp();
else $('#JECTvolumeshow').slideDown();
});
&#13;
如果您正在寻找比较该元素的文本内容的内容,那么使用jquery可能最容易,例如:
$("input[name=JECT]").click(function() {
if (this.value == "no" || this.value == "JECTindividual_electronic") || $("#JECTRenewal").text() == "yes")
$('#JECTvolumeshow').slideUp();
else $('#JECTvolumeshow').slideDown();
});
&#13;
答案 2 :(得分:1)
使用jquery时,你必须选择&#34;这个&#34;以jquery时尚 ex。 $(this);你也必须使用 .val()而不是值。
示例。
$("input[name=JECT]").click(function() {
if ( $(this).val() == "no" || $(this).val() == "JECTindividual_electronic" || $("#JECTRenewal").text() == "yes"){
$('#JECTvolumeshow').slideUp();
} else {
$('#JECTvolumeshow').slideDown();
}
});
我还在此代码中纠正了其他一些小问题......
希望这会有所帮助。