我正在修改一个HTML表单,以便在选中"Parent"框后有条件地显示问题的更多子集.我现在的方法运行得很好,但是有没有一种更简单的方法来做到这一点,这样你就不必像在这个例子中那样, for each "海报设计"重写函数.在不专门针对一个ID的情况下编写它的某种方式(可能是使用jQuery)?
我道歉,如果代码是混乱的或似乎缺乏经验-它是.我相信有框架可以让这变得更容易,但是我有点紧张要走出Vanilla JS,因为我有点把它放进一个现有的网站,我不确定使用的是什么框架.
而<table>
元素的随机使用只是因为我正在重新制作一位前队友的作品,而还没有将整个过程转换为<div>
.
//poster A
function randiPosterAShow() {
if (document.getElementById('posterA-NewStore').checked) {
document.getElementById('r-posterA-dropdown').style.display = 'block';
} else {
document.getElementById('r-posterA-dropdown').style.display = 'none';
}
}
//poster A 8.5 x 11
function randiPosterA85Show() {
if (document.getElementById('postera8511').checked) {
document.getElementById('r-posterA-85-dropdown').style.display = 'block';
} else {
document.getElementById('r-posterA-85-dropdown').style.display = 'none';
}
}
//poster A 11 x 17
function randiPosterA11Show() {
if (document.getElementById('postera1117').checked) {
document.getElementById('r-posterA-11-dropdown').style.display = 'block';
} else {
document.getElementById('r-posterA-11-dropdown').style.display = 'none';
}
}
//poster B
function randiPosterBShow() {
if (document.getElementById('posterB-SchOrders').checked) {
document.getElementById('r-posterB-dropdown').style.display = 'block';
} else {
document.getElementById('r-posterB-dropdown').style.display = 'none';
}
}
//poster B 8.5 x 11
function randiPosterB85Show() {
if (document.getElementById('posterb8511').checked) {
document.getElementById('r-posterB-85-dropdown').style.display = 'block';
} else {
document.getElementById('r-posterB-85-dropdown').style.display = 'none';
}
}
//poster B 11 x 17
function randiPosterB11Show() {
if (document.getElementById('posterb1117').checked) {
document.getElementById('r-posterB-11-dropdown').style.display = 'block';
} else {
document.getElementById('r-posterB-11-dropdown').style.display = 'none';
}
}
<table>
<tr>
<td style="width: 80px;" valign="top"><span>Design:</span></td>
<td>
<!-- poster A starts here -->
<input onclick="randiPosterAShow();" autocomplete="off" id="posterA-NewStore" name="posterA-NewStore" tabindex="1" title="Poster design new store" type="checkbox" value="posterA-NewStore" />
<label for="posterA-NewStore"><span>A (New Store)</span></label>
<!-- show from here down when "A" is checked. below div should be display:none -->
<div style="margin: 5px 0 0 25px; display: none;" id="r-posterA-dropdown">Size:
<div style="display: inline-block; vertical-align: top;">
<input onclick="randiPosterA85Show();" style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="postera8511" name="postera8511" tabindex="1" title="8.5 x 11" type="checkbox" value="postera8511" />
<label for="postera8511">8.5" x 11"</label>
<!-- show from here down when "A – 8.5 x 11" is checked. below div should be set to display:none -->
<div style="display: none; margin: 5px 0 5px 20px;" id="r-posterA-85-dropdown">Options:
<div style="display: inline-block; vertical-align: text-top;">
<input style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="postera85qr" name="postera85qr" tabindex="1" title="8.5 x 11" type="checkbox" value="postera85qr" />
<label for="postera85qr">Add QR Code</label>
<br><input style="margin-right: 5px !important;" autocomplete="off" id="postera85sp" name="postera85sp" tabindex="1" title="8.5 x 11" type="checkbox" value="postera85sp" />
<label for="postera85sp">Include Spanish Version</label>
</div>
</div>
<div>
<input onclick="randiPosterA11Show();" style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="postera1117" name="postera1117" tabindex="1" title="8.5 x 11" type="checkbox" value="postera1117" />
<label for="postera1117">11" x 17"</label>
<!-- show from here down when "A – 11 x 17" is checked. below div should be set to display:none -->
<div style="display: none; margin: 5px 0 5px 20px;" id="r-posterA-11-dropdown">Options:
<div style="display: inline-block; vertical-align: text-top;">
<input style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="postera11qr" name="postera11qr" tabindex="1" title="8.5 x 11 QR Code" type="checkbox" value="postera11qr" />
<label for="postera11qr">Add QR Code</label>
<br><input style="margin-right: 5px !important;" autocomplete="off" id="postera11sp" name="postera11sp" tabindex="1" title="8.5 x 11 QR Code" type="checkbox" value="postera11sp" />
<label for="postera11sp">Include Spanish Version</label>
</div>
</div>
</div>
</div>
</div>
<!-- poster A ends here -->
<!-- poster B starts here -->
<div style="margin-top: 5px;">
<input onclick="randiPosterBShow();" autocomplete="off" id="posterB-SchOrders" name="posterB-SchOrders" tabindex="1" title="Poster design scheduled orders" type="checkbox" value="posterB-SchOrders" />
<label for="posterB-SchOrders"><span>B (Scheduled Orders)</span></label>
<!-- show from here down when "B" is checked. below div should be display:none -->
<div style="margin: 5px 0 0 25px; display: none;" id="r-posterB-dropdown">
Size:
<div style="display: inline-block; vertical-align: top;">
<input onclick="randiPosterB85Show();" style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="posterb8511" name="posterb8511" tabindex="1" title="8.5 x 11" type="checkbox" value="posterb8511" />
<label for="posterb8511">8.5" x 11"</label>
<!-- show from here down when "A – 8.5 x 11" is checked. below div should be set to display:none -->
<div style="display: none; margin: 5px 0 5px 20px;" id="r-posterB-85-dropdown">Options:
<div style="display: inline-block; vertical-align: text-top;">
<input style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="posterb85qr" name="posterb85qr" tabindex="1" title="8.5 x 11" type="checkbox" value="posterb85qr" />
<label for="posterb85qr">Add QR Code</label>
<br><input style="margin-right: 5px !important;" autocomplete="off" id="posterb85sp" name="posterb85sp" tabindex="1" title="8.5 x 11" type="checkbox" value="posterb85sp" />
<label for="posterb85sp">Include Spanish Version</label>
</div>
</div>
<div>
<input onclick="randiPosterB11Show();" style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="posterb1117" name="posterb1117" tabindex="1" title="8.5 x 11" type="checkbox" value="posterb1117" />
<label for="posterb1117">11" x 17"</label>
<!-- show from here down when "A – 11 x 17" is checked. below div should be set to display:none -->
<div style="display: none; margin: 5px 0 5px 20px;" id="r-posterB-11-dropdown">Options:
<div style="display: inline-block; vertical-align: text-top;">
<input style="margin-right: 5px !important; vertical-align: text-top;" autocomplete="off" id="posterb11qr" name="posterb11qr" tabindex="1" title="11 x 17 QR Code" type="checkbox" value="posterb11qr" />
<label for="posterb11qr">Add QR Code</label>
<br><input style="margin-right: 5px !important;" autocomplete="off" id="posterb11sp" name="posterb11sp" tabindex="1" title="11 x 17 Spanish" type="checkbox" value="posterb11sp" />
<label for="posterb11sp">Include Spanish Version</label>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- poster B ends here -->
<div style="margin-top: 5px;">
<input autocomplete="off" id="posterC-Topic" name="posterC-Topic" tabindex="1" title="Poster design topic" type="checkbox" value="posterC-Topic" />
<label for="posterC-Topic"><span>C (Placeholder Topic)</span></label>
</div>
</td>
</tr>
</table>