gpt4 book ai didi

javascript - 如何使用 Bootstrap 使表格内的单选按钮看起来像按钮?

转载 作者:行者123 更新时间:2023-12-04 09:30:52 24 4
gpt4 key购买 nike

我无法解决问题 - 还 - 关于 bootstrap ,但我希望有人能给我一些建议来解决它:)
我有一个表格,我可以在其中显示用户可以选择的不同选项。我使用单选按钮来显示不同的选项,因为我只想选择其中一个选项。
我在使用单选按钮时遇到的问题是我不想显示圆圈。
这是我所拥有的
What I have
这是我想要的
what I want
我试图放置一个包含一行按钮的 div 来应用 class="btn-group btn-group-toggle" data-toggle="buttons"但我没有让它工作。如果我在 <tr> 中编写该代码标签,它可以工作,但上面的行移到了屏幕的右侧。
另外,我尝试使用 css 来隐藏圆圈

input[type=radio]{
visibility: hidden;
}
但按钮内的文字没有居中
非常感谢您的时间和帮助!
这是我的代码:
<table class="table table-responsive">
<thead class="thead-light">
<tr>
<th scope="col">Zona Sísmica</th>
<th scope="col">I</th>
<th scope="col">II</th>
<th scope="col">III</th>
<th scope="col">IV</th>
<th scope="col">V</th>
<th scope="col">VI</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Valor factor Z</th>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<td>
<label class="btn btn-outline-primary">
<input type="radio" name="z" id="Zop1" value="0.15"> 0.15
</label>
</td>
<td>
<label class="btn btn-outline-primary">
<input type="radio" name="z" id="Zop2" value="0.25"> 0.25
</label>
</td>
<td>
<label class="btn btn-outline-primary">
<input type="radio" name="z" id="Zop3" value="0.30"> 0.30
</label>
</td>
<td>
<label class="btn btn-outline-primary">
<input type="radio" name="z" id="Zop4" value="0.35"> 0.35
</label>
</td>
<td>
<label class="btn btn-outline-primary">
<input type="radio" name="z" id="Zop5" value="0.40"> 0.40
</label>
</td>
<td>
<label class="btn btn-outline-primary">
<input type="radio" name="z" id="Zop6" value="0.50"> &ge;0.50
</label>
</td>
</div>
</tr>
</tbody>
</table>

最佳答案

这是基于 this tutorial 的解决方案.
您绝对可以通过使用他们提出的其他一些建议(例如 :hover:focus 上的样式)来改进它——出于美观和可访问性的原因,我建议这样做。
单选按钮被隐藏 opacity: 0width: 0 ,标签覆盖它们是因为 position: fixed (这需要将 position 设置为除 static 以外的其他内容的祖先)
因为 标签紧跟在输入元素 之后在标记中,当隐藏输入通过 .better-radio:checked + label 被选中/取消选中时,它可以动态设置样式。 .

.better-radio {
opacity: 0;
position: fixed;
width: 0;
}
label {
display: inline-block;
padding: 10px 20px;
border: 1px solid black;
border-radius: 4px;
background-color: lightgrey;
}
.better-radio:checked + label {
background-color: lightgreen;
border-color: green;
}
<table class="table table-responsive">
<thead class="thead-light">
<tr>
<th scope="col">Zona Sísmica</th>
<th scope="col">I</th>
<th scope="col">II</th>
<th scope="col">III</th>
<th scope="col">IV</th>
<th scope="col">V</th>
<th scope="col">VI</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Valor factor Z</th>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<td>
<input class="better-radio" type="radio" name="z" id="zop1" value="0.15" />
<label for="zop1" class="btn btn-outline-primary">0.15</label>
</td>
<td>
<input class="better-radio" type="radio" name="z" id="zop2" value="0.25" />
<label for="zop2" class="btn btn-outline-primary">0.25</label>
</td>
<td>
<input class="better-radio" type="radio" name="z" id="zop3" value="0.30" />
<label for="zop3" class="btn btn-outline-primary">0.30</label>
</td>
<td>
<input class="better-radio" type="radio" name="z" id="zop4" value="0.35" />
<label for="zop4" class="btn btn-outline-primary">0.35</label>
</td>
<td>
<input class="better-radio" type="radio" name="z" id="zop5" value="0.40" />
<label for="zop5" class="btn btn-outline-primary">0.40</label>
</td>
<td>
<input class="better-radio" type="radio" name="z" id="zop6" value="0.50" />
<label for="zop6" class="btn btn-outline-primary">&ge;0.50</label>
</td>
</div>
</tr>
</tbody>
</table>

关于javascript - 如何使用 Bootstrap 使表格内的单选按钮看起来像按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62853031/

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