本文实例讲述了js显示下拉列表框内全部元素的方法。分享给大家供大家参考。具体如下:
3 a/ q! a* r4 Z( m: ?下面的js代码可以通过alert框显示指定下拉列表的全部元素! @# m9 m7 P: i* `# y- X6 {! {) a
<!doctype html>
+ a: n/ j# y& c' E: {( T8 B5 D" ?<html>
5 r; T3 [& Q& o8 O/ B<head>4 A- R( d; K) m5 [2 E c; {
<script>
. A5 O: h7 ~; S( B4 _function getoptions()
) Q2 _( k5 M. V% G8 t* A; a{5 L. J+ u' d4 n! H% A+ {
var x=document.getelementbyid(myselect);
7 O* x9 O+ v, J' hvar txt=all options: ;
$ a, X1 u8 i0 i2 ivar i;. o, G! s7 U) j# T) B7 e8 A
for (i=0;i<x.length;i++)' r6 y- g/ K! S( D# a1 [: e' U* i
{& h' f: K7 L d% r8 z% [
txt=txt + \n + x.options[i].text;" i7 W* d, E; c( ^) \" M
}
1 g6 K, a6 R. ?9 ?/ Y( `) M/ oalert(txt);
+ u( y V3 c9 [; Q1 S5 x}/ J3 t/ Q0 T% o1 H4 `9 P8 F/ ]
</script>/ K3 L; L( U! D" }
</head>
( r4 E3 y* Y9 U1 Y2 ^$ x<body>7 r4 K; Z+ D y" @8 Z/ B
<form> B+ T7 Q+ f0 S, c0 J% F' q. u+ s' y8 Q
select your favorite fruit:
6 I& ^2 P1 p9 s. X* A+ }<select id=myselect>
9 D& q$ {" R' }0 P+ b4 ?. ]<option>apple</option>
: b& J ^+ ^6 E& l9 p<option>orange</option>
5 m( W( F2 @. ^: i; V7 o<option>pineapple</option>7 n' ]. u7 j* J8 b8 d0 v
<option>banana</option>
. ^. k# P6 I \6 z1 H: ^</select>+ l" m& f7 Q' z0 c( p
<br><br>
, l& a# O. s/ t+ ~- B& C. k: t<input type=button onclick=getoptions()5 L+ a; S7 k" ] O: n3 m
value=output all options>+ I6 D, o# O1 \, K
</form>
, _' k2 Y1 P, F/ w- |</body>5 K( x5 F- n! @
</html>
. l: k9 }( P" ]
' p6 K, g* d. W更多网页制作信息请查看: 网页制作 |
|