1. <tfoot id='ylGsF'></tfoot>
      <i id='ylGsF'><tr id='ylGsF'><dt id='ylGsF'><q id='ylGsF'><span id='ylGsF'><b id='ylGsF'><form id='ylGsF'><ins id='ylGsF'></ins><ul id='ylGsF'></ul><sub id='ylGsF'></sub></form><legend id='ylGsF'></legend><bdo id='ylGsF'><pre id='ylGsF'><center id='ylGsF'></center></pre></bdo></b><th id='ylGsF'></th></span></q></dt></tr></i><div id='ylGsF'><tfoot id='ylGsF'></tfoot><dl id='ylGsF'><fieldset id='ylGsF'></fieldset></dl></div>

      <small id='ylGsF'></small><noframes id='ylGsF'>

        • <bdo id='ylGsF'></bdo><ul id='ylGsF'></ul>
        <legend id='ylGsF'><style id='ylGsF'><dir id='ylGsF'><q id='ylGsF'></q></dir></style></legend>

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

        时间:2023-10-20
        <tfoot id='U8eKG'></tfoot>
          <tbody id='U8eKG'></tbody>
              <i id='U8eKG'><tr id='U8eKG'><dt id='U8eKG'><q id='U8eKG'><span id='U8eKG'><b id='U8eKG'><form id='U8eKG'><ins id='U8eKG'></ins><ul id='U8eKG'></ul><sub id='U8eKG'></sub></form><legend id='U8eKG'></legend><bdo id='U8eKG'><pre id='U8eKG'><center id='U8eKG'></center></pre></bdo></b><th id='U8eKG'></th></span></q></dt></tr></i><div id='U8eKG'><tfoot id='U8eKG'></tfoot><dl id='U8eKG'><fieldset id='U8eKG'></fieldset></dl></div>
              <legend id='U8eKG'><style id='U8eKG'><dir id='U8eKG'><q id='U8eKG'></q></dir></style></legend>

              • <bdo id='U8eKG'></bdo><ul id='U8eKG'></ul>
                1. <small id='U8eKG'></small><noframes id='U8eKG'>

                  本文介绍了如何使用 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
                  }
                  

                  还可以查看 这个小提琴

                  这篇关于如何使用 javascript 获取所选单选按钮的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:IE HTML 单选更改事件 下一篇:AngularJs:ng-model 未绑定到 ng-checked 输入类型 =“radio",与 ng-r

                  相关文章

                    <tfoot id='qq42e'></tfoot>
                      <bdo id='qq42e'></bdo><ul id='qq42e'></ul>
                    1. <legend id='qq42e'><style id='qq42e'><dir id='qq42e'><q id='qq42e'></q></dir></style></legend>

                      <small id='qq42e'></small><noframes id='qq42e'>

                      <i id='qq42e'><tr id='qq42e'><dt id='qq42e'><q id='qq42e'><span id='qq42e'><b id='qq42e'><form id='qq42e'><ins id='qq42e'></ins><ul id='qq42e'></ul><sub id='qq42e'></sub></form><legend id='qq42e'></legend><bdo id='qq42e'><pre id='qq42e'><center id='qq42e'></center></pre></bdo></b><th id='qq42e'></th></span></q></dt></tr></i><div id='qq42e'><tfoot id='qq42e'></tfoot><dl id='qq42e'><fieldset id='qq42e'></fieldset></dl></div>