gpt4 book ai didi

javascript - 在 html 中隐藏/显示表单?

转载 作者:行者123 更新时间:2023-11-30 10:47:29 25 4
gpt4 key购买 nike

我是 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/

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