JS How to obtain the value of radio selected and radio value _javascript tips

Source: Internet
Author: User
Tags radarr
The following is an online-picked article (not tested but canonical to imitate)
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>text</title>
<script>
var chk = 0;
Window.onload=function () {
var chkobjs = document.getelementsbyname ("Radio");
for (Var i=0;i<chkobjs.length;i++) {
if (chkobjs[i].checked) {
CHK = i;
Break
}
}
}
function Check_radio () {
var chkobjs = document.getelementsbyname ("Radio");
for (Var i=0;i<chkobjs.length;i++) {
if (chkobjs[i].checked) {
if (chk = i) {
Alert ("Radio value does not change cannot be submitted");
Break
}
}
}
}
</script>
<body>
<form action= ' method= ' post ' onsubmit= ' Javascript:return check_radio () ' >
<input type= ' Radio ' value= ' 1 ' name= ' radio, ' checked= ' checked ' > A;
<input type= ' Radio ' value= ' 2 ' name= ' Radio ' > two;
<input type= ' Radio ' value= ' 3 ' name= ' Radio ' > three;
<input type= ' Radio ' value= ' 4 ' name= ' Radio ' > four;
<input type= ' Radio ' value= ' 5 ' name= ' Radio ' > five;

<input Type=submit value=sub >
</form>
</body>

The following is a value that does not choose to get radio
Copy Code code as follows:

<input type= "Radio" name= "Money" value= "1"/> USD
<input type= "Radio" name= "Money" value= "2"/> yen
<input type= "Radio" name= "Money" value= "3"/> Euro

Native JS mode: (native DOM operation will be the text as a node, so there will be nextsibling)
Copy Code code as follows:

var dollar = document.getelementsbyname ("Money") [0].nextsibling.nodevalue;
var yen = Document.getelementsbyname ("Money") [1].nextsibling.nodevalue;
var € = document.getelementsbyname ("Money") [2].nextsibling.nodevalue;

jquery Way
Copy Code code as follows:

$ (' input[name= ' money ']:checked '). Next (' span '). HTML ();

<input type= "Radio" name= "Money" value= "1" checked= "checked"/><span> USD </span>
<input type= "Radio" name= "Money" value= "2"/><span> yen </span>
<input type= "Radio" name= "Money" value= "3"/><span> Euro </span>

<!--normal, create a check box or a radio box to link it with a label, such as:-->
<input id= "Radio1" type= "Radio" name= "Money" value= "1"/><label for= "Radio1" > USD </label>

The following to select:

This only judge has no choice
Copy Code code as follows:

function Radiovalue () {
var Radarr = document.getelementsbyname ("Radiov");
var radvalue = "";
alert (radarr.length);
for (var i=0; i<radarr.length; i++) {
Alert (radarr[i].checked+ "" +radarr[i].name + "" + Radarr[i].value);
if (radarr[i].checked) {
Radvalue = Radarr[i].value;

}
}

if (radvalue!= null && radvalue!= "") {
alert (Radvalue);
}else{
Alert ("Please select");
}

}

<input type= "button" value= "Test radio Get Data" onclick= "Radiovalue ();" />

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.