gpt4 book ai didi

javascript - 如何将复选框水平对齐并在单击时显示表单?

转载 作者:行者123 更新时间:2023-12-01 02:12:23 24 4
gpt4 key购买 nike

我一直在 stackoverflow 上搜索,但是找不到与我的问题相关的解决方案。我想要的是当用户单击复选框时,会显示一个表单。我设法获得一个复选框并显示表单,但无法将复选框并排放置。所以我想要的是:

[] residential   [] commercial    [] industrial.  

当用户单击上述框时,会在同一页面上打开一个表单。我的代码:

编辑:感谢@brk提供的解决方案。复制下面的解决方案后,出于某种原因,当我单击“住宅”时,我没有看到任何弹出的内容。我的代码:

<script>
// Add event listener to each of the radio button
document.querySelectorAll('input[name="selectType"]').forEach(function(item) {
item.addEventListener('click', function(e) {
// get the data-type from this, this data-type will be used to
// hide show relevant form
let name = this.getAttribute('data-type');
// hide previously displayed form
if (document.querySelector('.formDisplay') !== null) {
document.querySelector('.formDisplay').classList.remove('formDisplay');
}
// find the form whose name+_info is same as the name of the form
// & add a formDisplay to it
document.querySelector('form[name="' + name + '_info"]').classList.add('formDisplay');

})
})
</script>

<style>

form {
display: none;
}

.formDisplay {
display: block;
}

</style>

<html>

<body>
<div class="form-group">
<input id="checkboxR" data-type='res' name='selectType' type='radio'>
<label for="checkboxR">Residential</label>
<input id="checkboxC" data-type='com' name='selectType' type='radio'>
<label for="checkboxC">Commercial</label>
<input id="checkboxI" data-type='ind' name='selectType' type='radio'>
<label for="checkboxI">Industrail</label>
</div>

<form name="res_info" action="/result/" method="get" class="form-horizontal" role="form">
Residential Form
</form>
<form name="com_info" action="/result/" method="get" class="form-horizontal" role="form">
Commercial Form
</form>
<form name="ind_info" action="/result/" method="get" class="form-horizontal" role="form">
Industrial Form
</form>
</body>

</html>

所以,我看到 3 个单选按钮,但单击它们不会打开任何内容。抱歉,我对 html/css 有点陌生,所以这个概念是新的。

最佳答案

在 html 中进行更改并添加另一个属性 data-type 。该值将用于显示相应的表单。

创建 tep css 并最初隐藏所有表单。添加另一个类以使用 display:block

显示表单

单击单选按钮获取数据类型查找名称与该数据类型匹配的表单并显示该表单

// Add event listener to each of the radio button
document.querySelectorAll('input[name="selectType"]').forEach(function(item) {
item.addEventListener('click', function(e) {
// get the data-type from this, this data-type will be used to
// hide show relevant form
let name = this.getAttribute('data-type');
// hide previously displayed form
if (document.querySelector('.formDisplay') !== null) {
document.querySelector('.formDisplay').classList.remove('formDisplay');
}
// find the form whose name+_info is same as the name of the form
// & add a formDisplay to it
document.querySelector('form[name="' + name + '_info"]').classList.add('formDisplay');

})
})
form {
display: none;
}

.formDisplay {
display: block;
}
<body>
<div class="form-group">
<input id="checkboxR" data-type='res' name='selectType' type='radio'>
<label for="checkboxR">Residential</label>
<input id="checkboxC" data-type='com' name='selectType' type='radio'>
<label for="checkboxC">Commercial</label>
<input id="checkboxI" data-type='ind' name='selectType' type='radio'>
<label for="checkboxI">Industrail</label>
</div>

<form name="res_info" action="/result/" method="get" class="form-horizontal" role="form">
Residential Form
</form>
<form name="com_info" action="/result/" method="get" class="form-horizontal" role="form">
Commercial Form
</form>
<form name="ind_info" action="/result/" method="get" class="form-horizontal" role="form">
Industrial Form
</form>
</body>

关于javascript - 如何将复选框水平对齐并在单击时显示表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49663816/

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