JS gets a simple example of the value of a text box, a Drop-down box, a _javascript trick

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

Copy Code code as follows:

<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 (options[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.
Copy Code code as follows:

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.

Copy Code code as follows:

<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>


JS Gets the value and text of the selected item in the dropdown box

Firefox and IE gets the value and text of the selected item in the dropdown box:

1.IE and Firefox are supported methods:

Get text

Copy Code code as follows:

var Obj=document.getelementbyid (' Select_template ');
var text=obj.options[obj.selectedindex].text;//get text

var Obj=document.getelementbyid ("Select_template");

The length of the for (i=0;i<obj.length;i++) {//dropdown box is the number of his options

if (obj[i].selected==true) {

var text=obj[i].text;//get text

}
}


Compared to the previous method is relatively simple

1.IE support Firefox does not support:

Copy Code code as follows:

var Obj=document.getelementbyid (name);

for (i=0;i<obj.length;i++) {
if (obj[i].selected==true) {
var text= obj[i].innertext;
}
}


Get value method IE and Firefox universal:
var Value=document.getelementbyid ("Select_template"). value;//Get Value

Summary: In fact, the main is IE and Firefox support the value and Text properties, Firefox does not support innertext properties.

JS to implement the current page to open a new link:
Window.location.href=url;

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.