【问题标题】:selecting radio buttons in a function在函数中选择单选按钮
【发布时间】:2016-10-21 20:11:56
【问题描述】:

所以这里我有一个带有单选按钮和提交输入的 html 表单。我遇到的问题是选择这些无线电输入并在事件函数中提醒选中的输入。

      <div id="shippingMethod">
            <label>Shipping Method:</label><br>
            <input type="radio" value="free Mail" name="r_method" id="freeMail" checked>      <label for="freeMail">Free Mail (1 Month)</label><br>

            <input type="radio" value="UPS" name="r_method" id="ups">
            <label for="ups">UPS (1 week)</label><br>

            <input type="radio" value="DHL" name="r_method" id="dhl">
            <label for="dhl">DHL (2-4 days)</label>
            <input type="submit" value="enter">
        </div>
        <script type="text/javascript">
            document.getElementById('shopping').addEventListener('submit', actions);
            function actions(event){
                event.preventDefault();

                var method = document.getElementById('shopping').r_method;
                for (var i = 0; i < method.length; i++){
                    if(method[i].checked == "true"){
                        alert(method[i].value);
            }
            }
            }
        </script>

每次我进入控制台并输入“方法”时,它都会显示“未捕获的 ReferenceError:方法未定义”,但是我在操作函数中定义了方法。当我每次按下提交输入时,也没有任何反应。但是,当我在控制台日志中创建此变量“方法”时。有用。 我该如何解决这个问题?

【问题讨论】:

  • 什么是r_method? ID shopping 的元素是什么?
  • r_method 是每个无线电输入的名称,有一个表单 ID,它实际上是“购物”,但我错误地认为它没有添加,所以想象 div id="shippingMethod" 在表单中带有 id="shopping"
  • 这不仅仅是为了获取单选按钮的值,更多的是为什么这段代码在事件监听器的函数中不起作用,尽管代码似乎是正确的。

标签: javascript dom


【解决方案1】:

试试这个

document.getElementById('shippingMethod').addEventListener('submit', actions);
function actions(event){
  event.preventDefault();
  alert(document.querySelector('input[name="r_method"]:checked').value)
}
<form id="shippingMethod">
    <label>Shipping Method:</label><br>
    <input type="radio" value="free Mail" name="r_method" id="freeMail" checked>      <label for="freeMail">Free Mail (1 Month)</label><br>

    <input type="radio" value="UPS" name="r_method" id="ups">
    <label for="ups">UPS (1 week)</label><br>

    <input type="radio" value="DHL" name="r_method" id="dhl">
    <label for="dhl">DHL (2-4 days)</label>
    <input type="submit" value="enter">
</form>

编辑

错误:

  • 您的 HTML 代码中没有 form 标记。事件submit 永远不会被触发,也永远不会调用你的actions 函数
  • 您选择输入标签的方式应该是这样的: document.getElementById('shippingMethod').querySelectorAll('input[name="r_method"]'); 而不是 document.getElementById('shopping').r_method;。这样您就可以遍历包含所有元素的var method

使用 for 循环的工作示例是

document.getElementById('shippingMethod').addEventListener('submit', actions);
function actions(event){
  event.preventDefault();
  var method = document.getElementById('shippingMethod').querySelectorAll('input[name="r_method"]');
  for (var i = 0; i < method.length; i++){
  	if(method[i].checked == true){
    	alert(method[i].value)
    }
  }
}
<form id="shippingMethod">
  <label>Shipping Method:</label><br>
  <input type="radio" value="free Mail" name="r_method" id="freeMail" checked>      <label for="freeMail">Free Mail (1 Month)</label><br>

  <input type="radio" value="UPS" name="r_method" id="ups">
  <label for="ups">UPS (1 week)</label><br>

  <input type="radio" value="DHL" name="r_method" id="dhl">
  <label for="dhl">DHL (2-4 days)</label>
  <input type="submit" value="enter">
</form>

看看这个HTML DOM querySelector() Method

【讨论】:

  • Thnx 这确实有效,但如果你能告诉我我的代码是否有错误,我将不胜感激,因为这对我来说似乎很合适,并且想要一个以相同方式的解决方案(如我正在使用香草javascript练习循环和选择输入等等)thnx
  • 我添加了一个基于您基于 for 循环的解决方案的示例。
  • 非常感谢,不胜感激,看来新的定位方式是查询选择器
猜你喜欢
  • 2016-05-03
  • 1970-01-01
  • 2021-10-21
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-04-26
  • 2017-07-24
  • 2011-09-22
相关资源
最近更新 更多