<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. 如何获取选中的单选按钮的值?

        How to get the selected radio button’s value?(如何获取选中的单选按钮的值?)
        <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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  keeping radio buttons checked after form submit(表单提交后保持选中单选按钮)
                  Bootstrap radio button quot;checkedquot; flag(Bootstrap 单选按钮“已选中旗帜)
                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                    <tbody id='Wua5b'></tbody>
                  1. <tfoot id='Wua5b'></tfoot>
                    • <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>