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

2022-01-21 00:00:00 text radio-button javascript

我有以下 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>

我正在尝试使用以下 javascript 检索与单选按钮关联的标签而不是值,但变量chosentime"仍然是未分配的".我通过在 alert(radios[i].innerhtml); 中输入一些警报来检查我的代码.抛出未分配的

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". I checked my code by throwing in a few alerts the 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);
}

不胜感激.提前致谢.

推荐答案

您必须单独访问相应的标签,因为它位于单独的标签中.因为标签的 for 属性等于其选项的 id,所以您可以轻松获取标签:

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(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
}

还可以查看 这个小提琴

相关文章