Home  >  Article  >  Web Front-end  >  Simple example of JS getting the value of text box, drop-down box and radio button_javascript skills

Simple example of JS getting the value of text box, drop-down box and radio button_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:58:181255browse

1. Text box

1.1

Assign the value to variable t through var t=document.getElementById("test").value,

1.2 Of course, you can also assign known variable values ​​​​to the text box in reverse, for example:

var m = "5";
document.getElementById("test").value= m;

2. Drop-down list box

2.1

Get the value selected in the such as "3" corresponding to the "text value" ("33") to the test text box, you can use the following method,

Copy code The code is as follows:

2.2 Compare the given value with the value in the If the given values ​​are the same, select it.



Copy code
The code is as follows:var m = "2", for(var i = 0;i                                                                                                           >                                                                                                                                               🎜>                                                                                                                                    Marquee

The name attribute value of a row of radio button boxes must be the same, so that radio selection can be achieved.





Copy code


The code is as follows:


aaaaaaaaaa

bbbbbbbbb




Js gets the value and sum of the selected item in the drop-down box Text

Get the value and text of the selected item in the drop-down box under Firefox and IE:

1. Methods supported by both IE and Firefox:

Get text

Copy code The code is as follows:

var obj=document.getElementById( 'select_template');
var text=obj.options[obj.selectedIndex].text;//Get text

var obj=document.getElementById("select_template");

for(i=0;i

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

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

}
}


Compared with the previous method, the two are simpler

1. IE supports Firefox but not:

Copy code The code is as follows:

var obj=document.getElementById(name);

for(i=0;i if(obj[i].selected==true) {
var text= obj[i].innerText;
}
}


The method of getting the value is common to IE and Firefox:
var value=document.getElementById("select_template").value;//Getting the value

Summary: In fact, the main reason is that both IE and Firefox support the value and text attributes, but Firefox does not support the innerText attribute.

Js implements opening a new link on the current page:
window.location.href=url;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn