简体   繁体   English

在功能中选择单选按钮

[英]selecting radio buttons in a function

so here I have a html form with radio buttons and a submit input. 所以在这里,我有一个带有单选按钮和提交输入的html表单。 The issue I am having is selecting these radio input and alerting the checked one in an event function. 我遇到的问题是选择这些无线电输入并在事件功能中提醒选中的那个。

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

everytime I go to the console and type "method" it says "Uncaught ReferenceError: method is not defined" however i defined method in the actions function. 每次我进入控制台并键入“ method”时,都会说“ Uncaught ReferenceError:方法未定义”,但是我在动作函数中定义了方法。 And when every I press the the submit input also nothing happens. 而且,每当我按下“提交”输入时,也不会发生任何事情。 however when I create this variable "method" in the console log. 但是,当我在控制台日志中创建此变量“方法”时。 it works. 有用。 how can i solve this issue? 我该如何解决这个问题?

Try this 尝试这个

 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> 

EDIT 编辑

Errors: 错误:

  • No form tag in your HTML code. 您的HTML代码中没有form标签。 The event submit will never be triggered and will never call your actions function 事件submit将永远不会触发,也永远不会调用您的actions功能
  • The way you are selecting your inputs tag should be something like this: document.getElementById('shippingMethod').querySelectorAll('input[name="r_method"]'); 您选择输入标签的方式应类似于: document.getElementById('shippingMethod').querySelectorAll('input[name="r_method"]'); instead of document.getElementById('shopping').r_method; 而不是document.getElementById('shopping').r_method; . This way you'll be able to loop through the var method containing all the elements. 这样,您将能够遍历包含所有元素的var method

A working example using the for loop is 使用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> 

Take a look at this HTML DOM querySelector() Method 看看这个HTML DOM querySelector()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM