javascript - How to get value of radio buttons that are separated by divs?
问题描述
I have 3 radio buttons, while they all share the same name
, for visual purposes, they're encapsulated in their own Divs
. The issue is, when I try to retrieve the value of the radio buttons (aka the selected button), it returns an error ReferenceError: editList is not defined
at HTMLButtonElement.document.getElementById.onclick (sqlTemplate.html:56)
Here is the code:
var a;
document.getElementById("submitButton").onclick = function(){
a = editList.value;
console.log(a);
}
#radioButtonCol1{
display: inline-block;
margin: 30px 30px;
border: 1px grey solid;
border-radius: 2px;
padding: 10px;
}
#radioButtonCol1 > *{
display: block;
}
#radioButtonCol1{
display: inline-block;
margin: 30px 30px;
width: 150px;
}
#radioButtonCol1 > *{
display: block;
}
#radioButtonCol1{
display: inline-block;
margin: 30px 30px;
}
#radioButtonCol1 > *{
display: block;
}
.radioInputStyle{
margin: 0 auto;
}
.radioLabelStyle{
}
#radioSubmitButtonDiv{
width: 100%;
margin-bottom: 30px;
}
#submitButton{
width: 174px;
display: block;
border-radius: 2px;
border: 1px solid grey;
margin: 0 auto;
}
#submitButton:active{
background-color: #D0D0D0;
}
<form action="/action_page.php" style="display: inline;">
<div id="radioGroupContainer">
<div id="radioButtonCol1">
<label for="always" class="radioLabelStyle">Linear</label>
<input id="radButLinear" type="radio" name="editList" value="always" class="radioInputStyle"/>
</div>
<div id="radioButtonCol1">
<label for="always" class="radioLabelStyle">Tactile</label>
<input id="radButTactile" type="radio" name="editList" value="always" class="radioInputStyle"/>
</div>
<div id="radioButtonCol1">
<label for="always" class="radioLabelStyle">Tactile and Clicky</label>
<input id="radButTandC" type="radio" name="editList" value="always" class="radioInputStyle"/>
</div>
</div>
</form>
<div id="radioSubmitButtonDiv">
<button id="submitButton">Submit</button>
</div>
How can I retrieve the value while keeping them separated?
解决方案
有几个问题。
首先,您在单选按钮上使用双 id,而第二个 id 很常见。因此,为所有人取消第二个 id 声明。
然后for
标签的属性应该指向id
每个输入的正确剩余部分。
然后,您value
对所有三个单选按钮都使用相同的选项,这无助于您了解用户选择的内容。
document.querySelector('.radioInputStyle:checked')
最后在您的脚本中,您需要在访问它之前查询选中的单选按钮value
所以
var a;
document.getElementById("submitButton").onclick = function(){
a = document.querySelector('.radioInputStyle:checked').value;
console.log(a);
}
#radioButtonCol1{
display: inline-block;
margin: 30px 30px;
border: 1px grey solid;
border-radius: 2px;
padding: 10px;
}
#radioButtonCol1 > *{
display: block;
}
#radioButtonCol1{
display: inline-block;
margin: 30px 30px;
width: 150px;
}
#radioButtonCol1 > *{
display: block;
}
#radioButtonCol1{
display: inline-block;
margin: 30px 30px;
}
#radioButtonCol1 > *{
display: block;
}
.radioInputStyle{
margin: 0 auto;
}
.radioLabelStyle{
}
#radioSubmitButtonDiv{
width: 100%;
margin-bottom: 30px;
}
#submitButton{
width: 174px;
display: block;
border-radius: 2px;
border: 1px solid grey;
margin: 0 auto;
}
#submitButton:active{
background-color: #D0D0D0;
}
<form action="/action_page.php" style="display: inline;">
<div id="radioGroupContainer">
<div id="radioButtonCol1">
<label for="radButLinear" class="radioLabelStyle">Linear</label>
<input id="radButLinear" type="radio" name="editList" value="linear" class="radioInputStyle"/>
</div>
<div id="radioButtonCol1">
<label for="radButTactile" class="radioLabelStyle">Tactile</label>
<input id="radButTactile" type="radio" name="editList" value="tactile" class="radioInputStyle"/>
</div>
<div id="radioButtonCol1">
<label for="radButTandC" class="radioLabelStyle">Tactile and Clicky</label>
<input id="radButTandC" type="radio" name="editList" value="both" class="radioInputStyle"/>
</div>
</div>
</form>
<div id="radioSubmitButtonDiv">
<button id="submitButton">Submit</button>
</div>
推荐阅读
- c# - 通过对数据表使用 Iqueryable 进行排序
- php - 当数组中有多个值时如何在php中显示未序列化的数据
- angular - 如何理解角度 ng-class 和动态类
- database - 在 PostgreSQL 中取消嵌套 JSON 对象列表
- postgresql - 有没有办法在 PostgreSQL 上使用带有 btree 索引的 pg_trgm 运算符?
- rxjs - 只有在使用 Pipeable Operators 时才有副作用?
- vba - 读取单元格日期时出错
- python - 停止在气流中执行剩余任务
- node.js - 保存 gpg 或 gpg2 的密码
- c# - 访问创建的文件 C#