How to use JavaScript to obtain the text box In the drop-down box, the corresponding values of a single sequence or assign them?
Xuanyuan tiger was released at: 2007-10-22
Code
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script language = "JavaScript" type = "text/JavaScript">
Function showalert ()
{
VaR MSG = Document. getelementbyid ("OK"). Value // remember to add double quotation marks to the ID!
Document. Write (MSG)
}
</SCRIPT>
</Head>
<Body>
<Input type = "text" id = "OK" name = "ok1"/>
<Input name = "aaaa" type = "Submit" id = "BT" onclick = "showalert ()" value = "Submit"/>
</Body>
</Html
1. Text Box
1.1 <input type = "text" name = "test" id = "test">
Use VaR T = Document. getelementbyid ("test"). Value to assign the value to variable t,
1.2 of course, you can also assign the known variable value to the 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>
Use VaR
S = Document. getelementbyid ("Sel"). Value gets the value selected in the <SELECT> box. The value = "2" option is selected by default, so the value is assigned to the variable S.
The value is "2", not "22 ",
If you want to assign the "value" such as "3" corresponding "text value" ("33") selected in <SELECT> to the test text box, 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
Compare the given value with the value in the <SELECT> box. If the value of <option> in <SELECT> is the same as the given value, select it.
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 region
The name attribute values of a single queue must be the same, so that you can select one option.
<Input type = "radio" name = ""
Value = "1"> aaaaaaaaaa <br>
<Input type = "radio" name = ""
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 ("bbbbbbbbbbb ");
}
}
// -->
</SCRIPT>