JavaScript gets the value of Select,checkbox,radio

Source: Internet
Author: User

1. Get and set the value of the Select,checkbox,radio

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<body>
check box:
<input type= "checkbox" Name= "CheckBox1" value= "1" checked/>1
<input type= "checkbox" Name= "CheckBox1" value= "2"/>2
<input type= "checkbox" Name= "CheckBox1" value= "3" checked/>3
<input type= "checkbox" Name= "CheckBox1" value= "4"/>4
<input type= "checkbox" Name= "CheckBox1" value= "5"/>5

<br/>

Radio Box:
<input type= "Radio" id= "Radio1" name= "Radio1" value= "1" checked>1
<input type= "Radio" id= "Radio1" name= "Radio1" value= "2" >2
<input type= "Radio" id= "Radio1" name= "Radio1" value= "3" >3
<br/>
Drop-down box:
<select id= "Select1" >
<option value= "1" >1</option>
<option value= "2" >2</option>
<option value= "3" >3</option>
</select>
<br/>

<button onclick= "Sub ();" > Submit </button><br/>
<button onclick= "fun1 (); > select 4,5 check box </button><br/>
<button onclick= "fun2 (); > select 3 Radio Box </button><br/>
<button onclick= "Fun3 (); > select 2 Radio Box </button><br/>
<script type= "Text/javascript" >
function Fun1 () {
check box setting value
var oCheckBox = document.getelementsbyname ("CheckBox1");
var arr = [];
var index = 0;
for (Var i=0;i<ocheckbox.length;i++)
{
if (Ocheckbox[i].value = = 4 | | ocheckbox[i].value = = 5)
{
Ocheckbox[i].checked = true;
}else{
ocheckbox[i].checked = false;
}
}
}

function fun2 () {
Radio Box Setting value
var Oradio = document.getelementsbyname ("Radio1");
for (Var i=0;i<oradio.length;i++)
{
if (Oradio[i].value = = 3)
{
Oradio[i].checked = true;
Break
}
}
}

function Fun3 () {
Drop-down box setting value
var oselect = document.getElementById ("Select1");
Oselect.value = 2;
}

function sub () {
check box to get the value
var oCheckBox = document.getelementsbyname ("CheckBox1");
var arr = [];
var index = 0;
for (Var i=0;i<ocheckbox.length;i++)
{
if (ocheckbox[i].checked)
{
arr[index++] = Ocheckbox[i].value;
}
}
Alert ("check box:" +arr.tostring ());

The radio box gets the value
var Oradio = document.getelementsbyname ("Radio1");
var rvalue = 0;
for (Var i=0;i<oradio.length;i++)
{
if (oradio[i].checked)
{
Rvalue = Oradio[i].value;
Break
}
}
Alert ("Radio box:" +rvalue);


var oselect = document.getElementById ("Select1");
Alert ("Drop-down box:" +oselect.value);
}
</script>
</body>

2. JQuery Method:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script type= "Text/javascript" src= "Jquery-1.4.2.min.js" ></script>

<body>
check box:
<input type= "checkbox" Name= "CheckBox1" value= "1" checked/>1
<input type= "checkbox" Name= "CheckBox1" value= "2"/>2
<input type= "checkbox" Name= "CheckBox1" value= "3" checked/>3
<input type= "checkbox" Name= "CheckBox1" value= "4"/>4
<input type= "checkbox" Name= "CheckBox1" value= "5"/>5

<br/>

Radio Box:
<input type= "Radio" id= "Radio1" name= "Radio1" value= "1" checked>1
<input type= "Radio" id= "Radio1" name= "Radio1" value= "2" >2
<input type= "Radio" id= "Radio1" name= "Radio1" value= "3" >3
<br/>
Drop-down box:
<select id= "Select1" >
<option value= "1" >1</option>
<option value= "2" >2</option>
<option value= "3" >3</option>
</select>
<br/>

<button onclick= "Sub ();" > Submit </button><br/>
<button onclick= "fun1 (); > select 4,5 check box </button><br/>
<button onclick= "fun2 (); > select 3 Radio Box </button><br/>
<button onclick= "Fun3 (); > select 2 Radio Box </button><br/>
<script type= "Text/javascript" >
$ (document). Ready (function () {

});
function Fun1 () {
check box setting value
var oCheckBox = $ ("input[name=checkbox1]");
var arr = [];
var index = 0;
for (Var i=0;i<ocheckbox.length;i++)
{
if (Ocheckbox[i].value = = 4 | | ocheckbox[i].value = = 5)
{
Ocheckbox[i].checked = true;
}else{
ocheckbox[i].checked = false;
}
}
}

function fun2 () {
Radio Box Setting value
var Oradio = $ ("input[name=radio1]");
for (Var i=0;i<oradio.length;i++)
{
if (Oradio[i].value = = 3)
{
Oradio[i].checked = true;
Break
}
}
}

function Fun3 () {
Drop-down box setting value
var oselect = $ ("#select1");
Oselect.val (2);
}

function sub () {
check box to get the value
var oCheckBox = $ ("input[name=checkbox1]");
var arr = [];
var index = 0;
for (Var i=0;i<ocheckbox.length;i++)
{
if (ocheckbox[i].checked)
{
arr[index++] = Ocheckbox[i].value;
}
}
Alert ("check box:" +arr.tostring ());

The radio box gets the value
var oradio =$ ("input[name=radio1]");
var rvalue = 0;
for (Var i=0;i<oradio.length;i++)
{
if (oradio[i].checked)
{
Rvalue = Oradio[i].value;
Break
}
}
Alert ("Radio box:" +rvalue);


var oselect = $ ("#select1");
Alert ("Drop-down box:" +oselect.val ());
}
</script>
</body>

JavaScript gets the value of Select,checkbox,radio

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.