分配单选按钮的标签而不是值(Assign radio button's label instead of value)
考虑html中的以下单选按钮:
<tr> <td> Lighting </td> <td> <label for="lighting1"> Off </label> <input id = "lighting1" name="lighting" type="radio" value="0"> </td> <td> <label for="lighting2"> Low </label> <input id = "lighting2" name="lighting" type="radio" value="1"> </td> <td> <label for="lighting3"> High </label> <input id = "lighting3" name="lighting" type="radio" value="2"> </td> </tr>
我需要一个javascript语句,它会将单选按钮的标签分配给变量而不是其值,即'关','低'或'高'而不是'0','1'或'2'。 当单选按钮被选中时。
这看起来很简单直接,但无论我尝试什么,我都无法实现它。 我还没有在论坛上找到任何工作答案。 请允许我说明所有不可行的试验,并仅使用一行代码就能启发我。
Consider the following radio buttons in html:
<tr> <td> Lighting </td> <td> <label for="lighting1"> Off </label> <input id = "lighting1" name="lighting" type="radio" value="0"> </td> <td> <label for="lighting2"> Low </label> <input id = "lighting2" name="lighting" type="radio" value="1"> </td> <td> <label for="lighting3"> High </label> <input id = "lighting3" name="lighting" type="radio" value="2"> </td> </tr>
I need a javascript statement that will assign the radio button's label on a variable instead of its value, ie 'Off', 'Low' or 'High' instead of '0', '1' or '2'. when the radio button is checked.
It seems so simple and straightforward yet I fail to achieve it no matter what I try. I haven't found any working answers on the forum either. Please spare me of stating all non workable trials and enlighten me with just a single line of code that works.
满意答案
您可以使用通用名称获取收音机,并使用
input.value = newValue
更新值function updateValues(){ var radios = document.querySelectorAll('[name="lighting"]'); for(var i = 0; i< radios.length; i++){ var id = radios[i].getAttribute('id'); radios[i].value = document.querySelector('label[for="'+id+'"]').textContent.trim() } } function registerEvents(){ var radios = document.querySelectorAll('[name="lighting"]'); for(var i = 0; i< radios.length; i++) radios[i].addEventListener("change", handleChange) } function handleChange(){ console.log(this.value) } registerEvents();
<tr> <td> Lighting </td> <td> <label for="lighting1"> Off </label> <input id="lighting1" name="lighting" type="radio" value="0"> </td> <td> <label for="lighting2"> Low </label> <input id="lighting2" name="lighting" type="radio" value="1"> </td> <td> <label for="lighting3"> High </label> <input id="lighting3" name="lighting" type="radio" value="2"> </td> </tr> <p id="selectedValues"></p> <button onclick="updateValues()" >Update Values</button>
You can fetch radios using common name and update values using
input.value = newValue
function updateValues(){ var radios = document.querySelectorAll('[name="lighting"]'); for(var i = 0; i< radios.length; i++){ var id = radios[i].getAttribute('id'); radios[i].value = document.querySelector('label[for="'+id+'"]').textContent.trim() } } function registerEvents(){ var radios = document.querySelectorAll('[name="lighting"]'); for(var i = 0; i< radios.length; i++) radios[i].addEventListener("change", handleChange) } function handleChange(){ console.log(this.value) } registerEvents();
<tr> <td> Lighting </td> <td> <label for="lighting1"> Off </label> <input id="lighting1" name="lighting" type="radio" value="0"> </td> <td> <label for="lighting2"> Low </label> <input id="lighting2" name="lighting" type="radio" value="1"> </td> <td> <label for="lighting3"> High </label> <input id="lighting3" name="lighting" type="radio" value="2"> </td> </tr> <p id="selectedValues"></p> <button onclick="updateValues()" >Update Values</button>
相关问答
更多使用单选按钮的HTML“标签”标签(Using the HTML 'label' tag with radio buttons)
检查单选按钮的标签的CSS选择器(CSS selector for a checked radio button's label)
在单选按钮上使用“标签”(Using “label for” on radio buttons)
单选按钮标签样式(Radio Button Label styling)
无法将单选按钮与标签分开(Not able to separate radio button from label)
获取单选按钮和标签文本的值放入两个TextField(Getting Value Of Radio Button And Label Text Put Into Two TextField)
单选按钮标签定位(Radio Button Label Positioning)
分配单选按钮的标签而不是值(Assign radio button's label instead of value)
标签文本重叠单选按钮(Label text overlapping radio button)
标签中间的单选按钮显示(radio button display in middle of label)
相关文章
更多jquery1.4.4的radio选中项
ios label 竖排设置
用‘button’跟‘text’组合代替‘file’,选择文件后点‘submit’,‘file’的值被清空
struts2 <s:checkbox>传值问题
Just Forget To Ask The Handbag's Name
Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years
HMTL 表单标签
《Sundy's 《Android深入浅出》《Android高级应用》《Android开发视频教程》》[MP4]
She’s Not Carrying A Handbag
Storm,yahoo!S4比较
最新问答
更多Python / IPython奇怪的不可重现列表索引超出范围错误(Python/IPython strange non reproducible list index out of range error)
故事板从表格单元中延伸出来并显示其详细信息披露按钮(storyboard segue from a tablecell an its Detail Disclosure Button)
我们可以将Gherkin功能文件与testcomplete集成(Can we integrate Gherkin feature files with testcomplete)
TrustAllCertificatesCallback被忽略(TrustAllCertificatesCallback is ignored)
返回嵌套元素的索引(Return index of nested element)
在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)
Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)
Javascript错误:未捕获TypeError:无法读取null的属性'value'[duplicate](Javascript error: Uncaught TypeError: Cannot read property 'value' of null [duplicate])
使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)
如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!