JavaScript Radio Value Get code _ form effects

Source: Internet
Author: User
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>javascript get Radio selected value </title> <script language=" javascript "type=" text/ JavaScript ">//copyright 2008//by Xiao Wu classmate qq:100052800//date 2008-11-25//$, used to get element ID name function $ (ID) {return docu Ment.getelementbyid (ID);} Gets the radio selected value function GetValue () {//Gets the container name of the radio and searches for the radio label input var radios = $ ("Radios") based on the container name. Getelementsbytag Name ("input"); Initialize object var grade = ""; Whether radio is selected, default unchecked, radio can specify checked= "checked" unaffected by flag var flag = false; Traverse for (Var i=0;i<radios.length;i++) {//If currently selected if (radios[i].checked = = true) {//Assigned GR, based on the number of input tags searched in the container (length) ade = Radios[i].value; Assignment flag = true; End loop break; }//Determine if radio has selected if (!flag) {alert ("No choose");Return }else{alert (grade); } </script> </pead> <body Microsoft Sans Serif ' "> <div id=" Radios "> <input name=" Radio " Type= "Radio" value= "1"/>1 <input name= "Radio" type= "Radio" value= "2"/>2 "<input" name= "Radio" type= "Radio" V Alue= "3"/>3 <input name= "Radio" type= "Radio" value= "4"/>4 <input name= "Radio" type= "Radio" value= "5"/> 5 <input name= "button" type= "button" value= "GetValue" onclick= "GetValue ()"/> </div> </body> </ptm L>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

JS Check Radio
Document.consumerform.consumer_sex[0].checked=true;
Here are some additional information on the Internet, I feel good, to provide you
This paper introduces the case that the Radio selected value can be conveniently taken by a function, which is applicable to two and above Radio with the same name. But in practice, there is only one Radio situation, so we have to enhance the program function, so that the program applies to one or more of the same name Radio.
<ptml> <pead> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> read Radio value </title> </pead> <body> <form id=" Form1 "action=" "Me Thod= "" > <div><input type= "Radio" name= "R" id= "R1" value= "one" ><label for= "R1" > Options one </label> </div> <div><input type= "button" value= "Check the selection" onclick= "Javascript:foo ();" ></div> </form> <script type= "Text/javascript" language= "JavaScript" > <!--function Foo () { var selectedindex =-1; var Form1 = document.getElementById ("Form1"); var i = 0; if (form1.r.value!= undefined) {//Only one radio condition if (form1.r.checked) {selectedindex = 0; Alert ("The value of your selected item is:" + form1.r.value); else {alert ("You did not select any item"); } return; For (i=0 i<form1.r.length; i++) {if (form1.r[i].checked) {selectedindex = i; Alert ("The value of your selected item is:" + form1.r[i].value); Break } if (SelectedIndex < 0) {alert ("You did not select any items"); }}--> </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.