如何隐藏表单并仅在选中时显示
How to hide a form and show up only when selected?
我想构建我的第一个程序。昨天我开始编码,我无法停止,但我在onchange
功能方面遇到了问题。我希望<form id="squareA">
仅在我的选项列表中选择Square时才显示。另一个列表也是如此。我还没有制作他们的表格,因为我被困在squareA。
这是我的代码。
<script>
function myShapes() {
var a1 = document.getElementById("shapes").value;
if(a1.value == option.value) {
document.getElementById("square").value = ("squareA");
} else {
document.getElementById("square").disabled = true;
}
}
}
</script>
<legend>Calculate Dimension</legend>
<select id="shapes" onchange="myShapes">
<option selected="selected" value="none">Select Shape</option>
<option onselect="mySquare" id="square" value="Square">Square</option>
<option onselect="myRect" id="rect" value="Rectangle">Rectangle</option>
<option onselect="myComb" id="comb" value="Combined">Combined</option>
<option onselect="myStrap" id="strap" value="Strap">Strap</option>
<option onselect="myTrap" id="trap" value="Trapezoidal">Trapezoidal</option>
</select>
<form id="squareA">
<div disabled="disabled" id="disa">
<input id="squareD" type="text" placeholder="Dimension"/> by
<input id="squareD2" type="text" placeholder="Dimension"/>
</div>
</form>
</fieldset>
长行内是不起作用或错误的代码。
你需要在 onchange
属性中调用函数,然后在更改处理程序中检查值并设置表单的显示值
function myShapes() {
/*On change check the value of the select and based on it set the display css value to either none or block*/
document.getElementById('squareA').style.display = document.getElementById('shapes').value == 'Square' ? 'block' : 'none'
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<legend>Calculate Dimension</legend>
<select id="shapes" onchange="myShapes()">
<!--Need to call the function here, id add () at the end-->
<option selected="selected" value="none">Select Shape</option>
<option value="Square">Square</option>
<option value="Rectangle">Rectangle</option>
<option value="Combined">Combined</option>
<option value="Strap">Strap</option>
<option value="Trapezoidal">Trapezoidal</option>
</select>
<form id="squareA" style="display:none;">
<!--Hide the form on page load-->
<div disabled="disabled" id="disa">
<input id="squareD" type="text" placeholder="Dimension" />by
<input id="squareD2" type="text" placeholder="Dimension" />
</div>
</form>
相关文章:
- 如何使用jquery在单击按钮时显示表单
- 如果选择单选按钮,则显示表单
- 单击按钮后在警报中显示表单值
- 提交后如何递增和显示表单数量
- 在HTML中隐藏/显示表单会产生异常结果
- 使用Jquery在单击“”时显示表单;添加对象”;按钮
- 根据发送的输入内容隐藏/显示表单
- 如何检查页面加载时是否选中复选框,如果选中,则显示表单
- 以引导模式显示表单结果
- 如何使用复选框显示表单的更多选项并更改表单的操作属性
- 根据在jQuery字段中输入的数量显示表单的不同部分
- 我正在尝试在使用 javascript 单击按钮时显示表单
- 如何使用 JavaScript 函数显示表单 asp.net
- 如何通过隐藏表单在同一页面中显示表单输出
- 如何向客户显示表单中的预览
- 如何在同一窗口中显示表单输出并隐藏表单
- 使用jQuery切换根据选中的复选框隐藏/显示表单提交按钮 - 如何设置默认关闭的提交按钮
- 显示表单前的 JQGRID 验证
- 根据下拉列表中的选定值显示表单
- Javascript:读取,添加和显示表单值