我有一个包含20个复选框的表单。
我想在选中复选框时显示div的内容。 我得到了这样的结果
#toggle-content1 {
display: none;
}
#mycheckbox1:checked~#toggle-content1 {
display: block;
height: 100px;
}
#toggle-content2 {
display: none;
}
#mycheckbox2:checked~#toggle-content2 {
display: block;
height: 100px;
}
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="0" />
<div id="toggle-content1">
This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="0" />
<div id="toggle-content2">
This content should appear when the checkbox is checked
</div>
但是,如果您尝试一下代码片段,则会注意到第一个复选框移动了第二个复选框(在打开div时),有一种方法可以将复选框保留为一行并在div上显示div。其他人?
css世界对我来说是陌生的,有一种方法可以为20个复选框编写该代码,而无需为每个div编写CSS?
答案 0 :(得分:3)
您可以尝试以下操作:
.content {
display: none;
width: 100%;
}
/*Use + to target only one .content*/
input[type=checkbox]:checked + .content {
display: block;
height: 50px;
}
.container {
display: flex;
flex-wrap: wrap;
}
input[type=checkbox] {
order: -1; /*make all the input on the top*/
}
<div class="container">
<input type="checkbox" name="mycheckbox" value="0">
<div class="content">
0)This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" value="1">
<div class="content">
1)This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" value="2">
<div class="content">
2)This content should appear when the checkbox is checked
</div>
<input type="checkbox" name="mycheckbox" value="3">
<div class="content">
3)This content should appear when the checkbox is checked
</div>
</div>
答案 1 :(得分:2)
我只是重新排序了您的标签,先输入然后再输入div。但是我不确定那是不是你想要的
#toggle-content1 {
display: none;
}
#mycheckbox1:checked~#toggle-content1 {
display: block;
height: 100px;
}
#toggle-content2 {
display: none;
}
#mycheckbox2:checked~#toggle-content2 {
display: block;
height: 100px;
}
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="0" />
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="0" />
<div id="toggle-content1">
This content should appear when the checkbox1 is checked
</div>
<div id="toggle-content2">
This content should appear when the checkbox2 is checked
</div>
答案 2 :(得分:1)
请在下面的更新代码中查看,希望您能找到解决方法。
.chkBox{
display:inline-block;
position:relative;
}
.chkTxt{
background:#fff;
padding:10px;
border:1px solid #ccc;
width:500px;
height:100px;
position:absolute;
top:100%;
left:0;
display:none;
}
.chkBox input[type="checkbox"]:checked + .chkTxt{
display:block;
}
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="0" />
<div class="chkTxt">This content should appear when the checkbox1 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="0" />
<div class="chkTxt">This content should appear when the checkbox2 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox3" value="0" />
<div class="chkTxt">This content should appear when the checkbox3 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox4" value="0" />
<div class="chkTxt">This content should appear when the checkbox4 is checked </div>
</div>
<div class="chkBox">
<input type="checkbox" name="mycheckbox" id="mycheckbox5" value="0" />
<div class="chkTxt">This content should appear when the checkbox5 is checked </div>
</div>
答案 3 :(得分:-2)
我将为此使用脚本...并且在实际应用程序中,我将使用模型+模板引擎。 (Vue engine sample) 这样,您可以将输出放置在所需的位置,并根据需要设置格式。这样,您还可以从服务器加载新的数据模型(将复选框问题/值/答案存储在db somwhere中),它将重新绘制/更新整个页面,而无需您进行任何更新。
// HTML
<input type="checkbox" name="mycheckbox" id="mycheckbox1" value="1" />
<input type="checkbox" name="mycheckbox" id="mycheckbox2" value="2" />
<input type="checkbox" name="mycheckbox" id="mycheckbox3" value="3" />
<input type="checkbox" name="mycheckbox" id="mycheckbox4" value="4" />
<div id="activeCheckboxes"></div><!-- you can put this anywhere on the page -->
// JavaScript
const checkboxes = {
1: {checked: false, message: 'First checkbox checked'},
2: {checked: false, message: 'Second checkbox checked'},
3: {checked: false, message: 'Third checkbox checked'},
4: {checked: false, message: 'Fourth checkbox checked'}
};
const activeCheckboxes = document.getElementById('activeCheckboxes');
const updateCheckboxes = () => {
activeCheckboxes.innerHTML = '';
Object.keys(checkboxes).forEach((val) => {
let checkbox = checkboxes[val];
if (checkbox.checked) {
activeCheckboxes.innerHTML += '<div>'+checkbox.message+'</div><hr>';
}
});
};
$(document).ready(() => {
$('input[type]="checkbox"').click((evt) => {
checkboxes[evt.target.value].checked = evt.target.checked;
updateCheckboxes();
});
});