gpt4 book ai didi

javascript - 选择元素时显示和隐藏表格行

转载 作者:行者123 更新时间:2023-11-30 09:42:21 26 4
gpt4 key购买 nike

无法让这个 HTML/java 脚本正常工作,已经工作了一天了。在线查看并找到我在这里编码的内容。我希望它在页面加载时不显示“Colors*”行。当我从下拉菜单中选择衬衫尺寸时,它会显示颜色选项。

截至目前,这有点奏效,其中包含颜色和单选按钮的表格会调整大小并且无法正确显示,与上面的行长度匹配,所以我被卡住了。连同单选按钮允许选择所有而不是一个。

我添加了两张图片来展示它的样子。编辑:按钮问题已修复

我希望只使用 javascript,而不使用 jquery。

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="jsForm.js"></script>
<title>Javascript with Forms</title>
<style type="text/css">
table{
width: 800px;
padding: 10px;
}
</style>
</head>
<body>
<form action="#">
<table border="1">
<caption>T-Shirt Selection</caption>
<tr><td style="100px;"></td><td style="150px;"></td><td style="50px;"></td><td style="50px;"></td><td></td></tr>
<tr>
<th>Description</th>
<th>Selection</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tr>
<td>
T_Shirt 1:
</td>
<td>
<select id="shirts" onchange="showButton(this)">
<option value="0" selected >Choose T-Shirt </option>
<option value="1" >Small $10.99</option>
<option value="2" >Medium $12.99</option>
<option value="3" >Large $14.99</option>
<option value="4" >X-Large $15.99</option>
<option value="5" >XX-Large $15.99</option>
</select>
</td>
<td>
<input type="text" name="qty" maxlength="4" size="4" />
</td>
<td>
<input type="text" name="qty" maxlength="10" size="10" />
</td>
</tr>


<tr id="hidden_button" style="display:none;">
<td colspan="1">Color.*</td>
<td colspan="3">
<input type="radio" name="Back" value="Black" /> Black
<input type="radio"name="White" value="White" />White
<input type="radio"name="Blue" value="Blue" />Blue
<input type="radio" name="Red" value="Red"/>Red
</td>
</tr> </table>
</form>
</body>

Javascript

function showButton(elem){
if(elem.value == 0){
document.getElementById('hidden_button').style.display = "none";
}else if(elem.value > 0){
document.getElementById('hidden_button').style.display = "block";
}

这就是我所说的 enter image description here enter image description here

非常感谢任何帮助

最佳答案

您需要在脚本中使用display = "table-row",而不是display = "block"

并且建议切换类而不是更改元素样式,如示例 2 中所示。

function showButton(elem) {
if (elem.value == 0) {
document.getElementById('hidden_button').style.display = "none";
} else if (elem.value > 0) {
document.getElementById('hidden_button').style.display = "table-row";
}
}
table {
width: 800px;
padding: 10px;
}
<form action="#">
<table border="1">
<caption>T-Shirt Selection</caption>
<tr>
<td style="100px;"></td>
<td style="150px;"></td>
<td style="50px;"></td>
<td style="50px;"></td>
<td></td>
</tr>
<tr>
<th>Description</th>
<th>Selection</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tr>
<td>
T_Shirt 1:
</td>
<td>
<select id="shirts" onchange="showButton(this)">
<option value="0" selected>Choose T-Shirt</option>
<option value="1">Small $10.99</option>
<option value="2">Medium $12.99</option>
<option value="3">Large $14.99</option>
<option value="4">X-Large $15.99</option>
<option value="5">XX-Large $15.99</option>
</select>
</td>
<td>
<input type="text" name="qty" maxlength="4" size="4" />
</td>
<td>
<input type="text" name="qty" maxlength="10" size="10" />
</td>
</tr>


<tr id="hidden_button" style="display:none;">
<td colspan="1">Color.*</td>
<td colspan="3">
<input type="radio" name="Back" value="Black" />Black
<input type="radio" name="White" value="White" />White
<input type="radio" name="Blue" value="Blue" />Blue
<input type="radio" name="Red" value="Red" />Red
</td>
</tr>
</table>
</form>

示例 2

function showButton(elem) {
if (elem.value == 0) {
document.getElementById('hidden_button').classList.remove('show');
} else if (elem.value > 0) {
document.getElementById('hidden_button').classList.add('show');
}
}
table {
width: 800px;
padding: 10px;
}

#hidden_button {
display: none;
}
#hidden_button.show {
display: table-row;
}
<form action="#">
<table border="1">
<caption>T-Shirt Selection</caption>
<tr>
<td style="100px;"></td>
<td style="150px;"></td>
<td style="50px;"></td>
<td style="50px;"></td>
<td></td>
</tr>
<tr>
<th>Description</th>
<th>Selection</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tr>
<td>
T_Shirt 1:
</td>
<td>
<select id="shirts" onchange="showButton(this)">
<option value="0" selected>Choose T-Shirt</option>
<option value="1">Small $10.99</option>
<option value="2">Medium $12.99</option>
<option value="3">Large $14.99</option>
<option value="4">X-Large $15.99</option>
<option value="5">XX-Large $15.99</option>
</select>
</td>
<td>
<input type="text" name="qty" maxlength="4" size="4" />
</td>
<td>
<input type="text" name="qty" maxlength="10" size="10" />
</td>
</tr>


<tr id="hidden_button">
<td colspan="1">Color.*</td>
<td colspan="3">
<input type="radio" name="Back" value="Black" />Black
<input type="radio" name="White" value="White" />White
<input type="radio" name="Blue" value="Blue" />Blue
<input type="radio" name="Red" value="Red" />Red
</td>
</tr>
</table>
</form>

关于javascript - 选择元素时显示和隐藏表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com