简体   繁体   English

如何使用 javascript 获取所选单选按钮的 label

[英]How do I get the label of the selected radio button using javascript

I have the following HTML我有以下 HTML

<div class="form-radios" id="edit-submitted-lunchset-lunch"><div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="1" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-1">  <label for="edit-submitted-lunchset-lunch-1" class="option">12:00 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="2" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-2">  <label for="edit-submitted-lunchset-lunch-2" class="option">12:30 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="3" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-3">  <label for="edit-submitted-lunchset-lunch-3" class="option">13:00 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="4" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-4">  <label for="edit-submitted-lunchset-lunch-4" class="option">13:30 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" value="5" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-5">  <label for="edit-submitted-lunchset-lunch-5" class="option">14:00 </label>

</div>
<div class="form-item form-type-radio form-item-submitted-lunchset-lunch">
<input type="radio" class="form-radio" checked="checked" value="6" name="submitted[lunchset][lunch]" id="edit-submitted-lunchset-lunch-6">  <label for="edit-submitted-lunchset-lunch-6" class="option">14:30 </label>

</div>
</div>

I am trying to retrieve the label associated with the radio button instead of the value using the following javascript but the variable "chosentime" is still "unassigned".我正在尝试检索与单选按钮关联的 label 而不是使用以下 javascript 的值,但变量“chosentime”仍然是“未分配的”。 I checked my code by throwing in a few alerts the alert(radios[i].innerhtml);我通过在 alert(radios[i].innerhtml); 中输入一些警报来检查我的代码。 throws unassigned抛出未分配

var radios = document.getElementsByName('submitted[lunchset][lunch]');
for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        alert(i);
        alert(radios[i].value);
        alert(radios[i].innerhtml);
        chosentime = radios[i].innerhtml;
    }
}
window.alert(chosentime);
}

Would appreciate any help.将不胜感激任何帮助。 Thanks in advance.提前致谢。

You have to access the corresponding label separately, since it resides in a separate tag. 您必须分别访问相应的标签,因为它位于单独的标签中。 Because the labels' for attribute is equal to its option's id , you can get to the label easily: 因为标签的for属性等于其选项的id ,所以您可以轻松进入标签:

for(var i=0; i<radios.length; i++) {
    var selector = 'label[for=' + radios[i].id + ']';
    var label = document.querySelector(selector);
    var text = label.innerHTML;
    // do stuff
}

Also check out this fiddle 还检查这个小提琴

A radiobutton have no innerhtml. 单选按钮没有innerhtml。 You need to target the label. 您需要定位标签。 If it's always directly after the radio-button, try something like radios[i].nextSibling.innerhtml . 如果总是在单选按钮之后,则尝试使用radios[i].nextSibling.innerhtml

Using modern Javascript and CSS this is easy now.使用现代 Javascript 和 CSS 现在很容易。

input.labels

Give your button an id , and then give the label a for='id' making sure it is the same id as the button.给你的按钮一个id ,然后给 label 一个for='id'确保它与按钮的id相同。

Buttons can have multiple labels so it returns a NodeList, if you only have 1 label, just take the first item in the list.按钮可以有多个标签,因此它返回一个 NodeList,如果您只有 1 个 label,则只需获取列表中的第一项。

radioButtonClicked.labels[0];

Docs here. 文档在这里。

Here is an example of how you would setup the HTML:以下是如何设置 HTML 的示例:

    <div class="controls">
        <input type="radio" name="select" id="large" class="radioButtons" checked>
        <input type="radio" name="select" id="medium" class="radioButtons">
        <input type="radio" name="select" id="small" class="radioButtons">
        <input type="radio" name="select" id="tiny" class="radioButtons">
       
        <label for="large" class="option option-1 checked">
            <span class="optionLabel">Large</span>
        </label>

        <label for="medium" class="option option-2">
            <span class="optionLabel">Medium</span>
        </label>

        <label for="small" class="option option-3">
            <span class="optionLabel">Small</span>
        </label>

        <label for="tiny" class="option option-4">
            <span class="optionLabel">Tiny</span>
        </label>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择单选按钮时,如何在标签上添加类别? - How do I add class to label when radio button is selected? 如何使用jQuery获取选定的单选按钮标签文本 - How to get the selected radio button label text using jQuery 如何在angularjs中获取所选单选按钮的值? - How do i Get value of selected radio button in angularjs? 如何使用 JavaScript 和 PHP 检查是否选择了单选按钮 - How can I check if a radio button is selected using JavaScript and PHP 如何更改使用功能选择的单选按钮? - How do I change which radio button is selected using a function? 如何检查是否使用JavaScript选择了单选按钮 - How to check if radio button is selected using javascript 如何使用jQuery或javascript使选定的单选按钮标签的文本加粗? - How to make the text of selected radio button label bold using jQuery or javascript? 如何使用 javascript 获取多个单选按钮具有相同名称的选定单选按钮的值 - How to get the value of selected radio button where multiple radio buttons have same name using javascript 当有多个具有相同名称的单选按钮时,如何检查选择了哪个单选按钮? (使用jquery) - How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery) 使用javascript获取相关的单选按钮标签文本 - get associated radio button label text using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM