Use JavaScript to get the text box, Drop-down box, the value of the radio box, and assign a value

Source: Internet
Author: User

1. Text box

1.1 <input type= "text" name= "test" id= "Test" >

The value is assigned to the variable t by the Var T=document.getelementbyid ("Test").

1.2 You can, of course, assign the value of a given variable to a text box, for example:

var m = "5";

document.getElementById ("Test"). value= m;



2. Drop-down list box

2.1 <select name= "sel" id= "sel" "onchange=" Look (); " >

<option value= "1" >11</option>

<option value= "2" selected>22</option>

<option value= "3" >33</option>

</select>

with Var S=document.getelementbyid ("sel"). Value gets the value selected in <select> box, where the value= "2" option is selected by default, so the value assigned to the variable s is "2" instead of "22",

If you want to assign a "value" such as "3" ("33") to the Test text box selected in <select>, you can use the following method

<script language= "JavaScript" >

function Look () {

var se =document.getelementbyid ("sel");
var option=se.getelementsbytagname ("option");
var str = "";
for (Var i=0;i<option.length;++i)
{
if (option[i].selected)
{
document.getElementById ("Test"). Value = Option[i].text;
}
}





}

</script>

2.2 Compares the given value to the value in the <select> box, select it if <option> 's value in <select> is the same as the given value.

var m = "2",

for (var i = 0;i<document.getelementbyid ("sel"). length;i++)
{
With (document.getElementById ("sel"). Options[i])
{
if (value = = m)
{
selected = true;
}
}
}



3. Single selection box

The name attribute value must be the same for a row of a single marquee, so that the radio can be implemented.

<input type= "Radio" Name= "a" value= "1" >aaaaaaaaaa<br>
<input type= "Radio" Name= "a" value= "2" >bbbbbbbbb<br>
<input type= "button" onclick= "Check ();" value= "Test" >
<script language= "JavaScript" >
<!--
function Check ()
{
var sel = 0;
for (var i = 0; i < Document.getelementsbyname ("a"). Length; i++)
{
if (Document.getelementsbyname ("a") [i].checked)
{
sel = Document.getelementsbyname ("a") [I].value;
}
}

if (sel = 1)
{
Alert ("Aaaaaaaaaa");
}
else if (sel== 2)
{
Alert ("BBBBBBBBB");
}
}
-->
</script>

Turn from: http://www.cnblogs.com/maizitongxue/archive/2009/03/13/1410890.html

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.