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

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

        <bdo id='mPFem'></bdo><ul id='mPFem'></ul>

    2. <legend id='mPFem'><style id='mPFem'><dir id='mPFem'><q id='mPFem'></q></dir></style></legend>
      <tfoot id='mPFem'></tfoot>
      1. 如何获取选中的单选按钮的值?

        时间:2023-10-21
        <tfoot id='YftqB'></tfoot>
      2. <i id='YftqB'><tr id='YftqB'><dt id='YftqB'><q id='YftqB'><span id='YftqB'><b id='YftqB'><form id='YftqB'><ins id='YftqB'></ins><ul id='YftqB'></ul><sub id='YftqB'></sub></form><legend id='YftqB'></legend><bdo id='YftqB'><pre id='YftqB'><center id='YftqB'></center></pre></bdo></b><th id='YftqB'></th></span></q></dt></tr></i><div id='YftqB'><tfoot id='YftqB'></tfoot><dl id='YftqB'><fieldset id='YftqB'></fieldset></dl></div>

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

              <tbody id='YftqB'></tbody>

                • <bdo id='YftqB'></bdo><ul id='YftqB'></ul>

                • <legend id='YftqB'><style id='YftqB'><dir id='YftqB'><q id='YftqB'></q></dir></style></legend>
                  本文介绍了如何获取选中的单选按钮的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 JS 程序出现了一些奇怪的问题.我让这个工作正常,但由于某种原因它不再工作.我只想找到单选按钮的值(选择了哪个)并将其返回给变量.由于某种原因,它一直返回 undefined.

                  I’m having some strange problem with my JS program. I had this working properly but for some reason it’s no longer working. I just want to find the value of the radio button (which one is selected) and return it to a variable. For some reason it keeps returning undefined.

                  这是我的代码:

                  function findSelection(field) {
                      var test = 'document.theForm.' + field;
                      var sizes = test;
                  
                      alert(sizes);
                          for (i=0; i < sizes.length; i++) {
                              if (sizes[i].checked==true) {
                              alert(sizes[i].value + ' you got a value');     
                              return sizes[i].value;
                          }
                      }
                  }
                  

                  提交表单:

                  function submitForm() {
                  
                      var genderS =  findSelection("genderS");
                      alert(genderS);
                  }
                  

                  HTML:

                  <form action="#n" name="theForm">
                  
                      <label for="gender">Gender: </label>
                      <input type="radio" name="genderS" value="1" checked> Male
                      <input type="radio" name="genderS" value="0" > Female<br><br>
                      <a href="javascript: submitForm()">Search</A>
                  </form>
                  

                  推荐答案

                  你可以这样做:

                  var radios = document.getElementsByName('genderS');
                  
                  for (var i = 0, length = radios.length; i < length; i++) {
                    if (radios[i].checked) {
                      // do whatever you want with the checked radio
                      alert(radios[i].value);
                  
                      // only one radio can be logically checked, don't check the rest
                      break;
                    }
                  }

                  <label for="gender">Gender: </label>
                  <input type="radio" name="genderS" value="1" checked="checked">Male</input>
                  <input type="radio" name="genderS" value="0">Female</input>

                  jsfiddle

                  感谢 HATCHA 和 jpsetung 的编辑建议.

                  Thanks HATCHA and jpsetung for your edit suggestions.

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

                  上一篇:Javascript函数 - 将字符串参数转换为运算符 下一篇:如何检查是否使用 JavaScript 选择了单选按钮?

                  相关文章

                  1. <tfoot id='Wua5b'></tfoot>
                  2. <small id='Wua5b'></small><noframes id='Wua5b'>

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