我是 html 的新手,我试图在用户勾选一个框时隐藏/显示表单,表单出现在其下方。
我有一个表格,例如
Name :
Username :
Add More Users: (tickbox)
如果用户勾选“添加更多用户”,则其下方会出现另一个包含“姓名、用户名、添加更多用户”字段的表单。这怎么可能?
最佳答案
HTML
<input type="checkbox" onclick="display(this)">Add more users</input>
<div id="yourDiv"> ...other forms... </div>
JavaScript
function display(e){
if (e.checked)
document.getElementById('yourDiv').style.display = 'block';
else
document.getElementById('yourDiv').style.display = 'none';
}
更好的方法(感谢 What)
HTML
<input id="more" type="checkbox">Add More Users</input>
<div id="yourDiv"> ...other form... </div>
JavaScript
window.onload = function () {
document.getElementById('more').onclick = function () {
if (this.checked)
document.getElementById('yourDiv').style.display = 'block';
else
document.getElementById('yourDiv').style.display = 'none';
}
}
关于javascript - 在 html 中隐藏/显示表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7401010/