本文实例讲述了js显示下拉列表框内全部元素的方法。分享给大家供大家参考。具体如下:0 G7 L1 i g2 w+ {
下面的js代码可以通过alert框显示指定下拉列表的全部元素, x7 l7 U' ^# i- c. C/ Y- D
<!doctype html>
, W4 w3 F' ]: P, ]& ?& u<html>
" j: n' g# c V5 b<head>
, w7 a3 b( j/ D5 T+ p( t g; h<script>* G5 s( E. \# {+ k Z6 P; |. |
function getoptions()
" j; U7 l2 ~1 o7 b3 K1 Z9 \{& O- [$ x z# Q) a4 S; r2 N
var x=document.getelementbyid(myselect);
L. J- f: U, R8 R* f, Evar txt=all options: ;
' A( Q7 t2 U4 D; I* K8 L( n% yvar i;+ U4 O; T) `3 J: D) |/ A
for (i=0;i<x.length;i++)5 U: ?, M- W. m
{
, f6 M A5 p/ X5 F6 j2 O1 {2 F! Otxt=txt + \n + x.options[i].text;
; V: o/ a6 ^& r; u) G}) q- L( i# z8 ~0 H, \4 u/ y, K
alert(txt);0 O! G! _! l T* ?6 d2 R
}. N8 Q; m9 \- |1 ^2 h7 Q- `
</script>
% B; s- X# ?$ x5 @! e0 @" @</head>) W4 W+ L5 Y- g3 X# j1 q7 I
<body>+ X) p' D# x. x1 i
<form>
3 _3 i! [, I0 `% x+ fselect your favorite fruit:
; H B0 H- p! M/ Z! O9 Q<select id=myselect>
! g; w9 T7 t" d<option>apple</option>" y- |0 |( G5 F! c+ x8 f
<option>orange</option>
3 [1 S" N$ p/ T M<option>pineapple</option>5 {/ J y0 u' \* O8 j" r1 ]9 d
<option>banana</option>: L f4 u0 f4 H& S2 }' N8 J. Q
</select>
# }8 |, t% H' z$ s) y8 z, r<br><br># P }* { y1 d" x6 t4 S# z
<input type=button onclick=getoptions()
; ^2 }0 f$ [9 @4 T1 J" T7 Kvalue=output all options>
( W9 O' F# T% _( N</form>3 N& ^+ v) u1 d% h; B( ]
</body>
: t/ ^" Q ]1 j* q- o8 r8 j! [</html># C* N0 T6 ~ ^0 x% e$ I7 f9 v7 E! ]
6 o5 N0 \" D3 R Q
更多网页制作信息请查看: 网页制作 |
|