本文实例讲述了js显示下拉列表框内全部元素的方法。分享给大家供大家参考。具体如下:
3 b. t$ e8 p: q: l! ^+ z下面的js代码可以通过alert框显示指定下拉列表的全部元素
6 s8 v6 b; x# q# ?5 \; U4 B) Q<!doctype html> ?9 |7 T4 \; O0 P- _; F
<html>
$ n0 v3 z, Y9 R" k! q<head>
( }! Z5 a1 Y6 @. Z/ @# S; u1 Y( _- O<script>- \& d5 x" c# J& S) s( H [
function getoptions(), ^# j* Z& b* J0 V$ G
{
# \, B- z* G$ z$ S% Mvar x=document.getelementbyid(myselect);
. V6 {+ P6 K/ O! P: Vvar txt=all options: ;
$ u+ C! {0 L5 m( R4 [* Nvar i;
0 a. j& M. k0 V/ Y6 hfor (i=0;i<x.length;i++)* I" Y" e6 i( { m5 u0 P0 Q
{
% x1 O+ P: w8 L; m, ?1 l2 \7 w) D0 V+ Ktxt=txt + \n + x.options[i].text;2 q1 Z! q) U( s
}" E0 ?. \6 ?$ V9 O$ M1 e- c( h9 E
alert(txt);
) z d2 @! w8 ?6 o5 E}
# L! Z' e6 [) B5 w</script>' [* p4 f. K! g. V% p5 j4 D
</head>
$ ]6 e' W$ X7 K" ^- W4 w! A2 G<body>4 J9 s7 E. \- f( ~7 J6 v l3 H
<form>
5 x& m w+ H: l! x, Z' Q; |3 Jselect your favorite fruit:* i) Z7 w! l* g8 d
<select id=myselect>
4 v( X$ e y7 B0 n2 T, }6 f7 A<option>apple</option>3 X6 w9 h- ` c# f3 S' T+ J; B
<option>orange</option>
7 y) G1 I: l* `* {9 [* | ?<option>pineapple</option>* |* r2 {4 O _5 `( p: d" G
<option>banana</option>8 V- U1 {4 z2 h* L7 W
</select>7 d) Z h7 W% ]; n% ?
<br><br>, z t, ]) V z" e4 [8 {
<input type=button onclick=getoptions()
) c. D ]1 D- q q4 uvalue=output all options>% |' c; p- y2 l- w/ s
</form>
2 r7 e# c+ f6 }$ x7 A9 w</body>
, X7 Q5 S& n' {9 Q" @! Y: Z</html>- S7 m$ F( }$ e
& |/ R5 g2 R7 e& r! z4 v
更多网页制作信息请查看: 网页制作 |
|