|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ l! }9 D+ B3 u5 h& v8 }
. t: Q/ j8 d$ w9 E
JS 操作 Select相关功能测试" T2 P$ n' l! K8 r% n; h% T, F
- P* r) c, f6 e) B[mw_shl_code=html,true]<html lang="en">
+ b2 S$ {* x6 U4 w
2 I0 r# x P) I* s; i<head>
. K. T" o" }, P# Y) [' u( a- K8 X <meta charset="UTF-8">
( ~/ N5 g- H2 q2 S! y$ B0 @1 k <meta name="viewport" content="width=device-width, initial-scale=1.0">
" E3 u9 N+ _7 K$ J9 r1 ? <meta http-equiv="X-UA-Compatible" content="ie=edge">2 x* b1 ]- {6 b/ q2 o! j
<title>Document</title>7 e# A- r7 _8 o& H
<style>
" m% c' a( ^& q1 B, y" I * {
) p8 N' K6 u" s: W. W* M" V2 t font-size: 10px;9 r. S: N& c) S5 u8 I! K
line-height: 24px;$ d2 t; S6 s! o% x
}
+ h8 c Y3 U' j: T7 ~4 S </style>
/ g l* q5 w, r</head>& y0 h( j/ {8 E1 K
) ^- c3 o4 p& F, Y/ R<body>& e+ z3 g m( ?* w* C# i/ @1 V
//this is for the select test/ V4 C, p& c& z8 \% H8 c7 f/ y
<form id='myForm' action="submit" method="GET">2 U6 H* F2 o2 B+ [1 m A4 N1 @3 W0 M
<input type="text" id="inpuTChange" />' b& o" g. C$ i- E6 G
<select name="selectFruits" id="mySelect">0 O' p1 { i8 @
<option value="Orange1">Orange</option>* D: h; Z" {! b0 J6 p* e- {9 h
<option value="Apple2">Apple</option>5 L$ A: [2 C. ?# e1 D
<option value="Banana3">Banana</option>5 N- G) D$ {' ~
</select>
( @- R5 I L& N3 M <br>
( y2 {+ L, G3 i$ f4 x# p <input type="button" value="disable">$ ]$ g* G8 z" x' |
<input type="button" value="Able">4 [! _* T$ U7 ?+ P* p
<input type="button" value="getValues">
0 i- A7 `# y8 b. w7 W <input type="button" value="editValues">
1 G0 F) n2 } o+ O: l2 w <input type="button" value="addValues">" l% d0 Z) j8 Q& D! i, Q @
<input type="button" value="removeValues"># c& k; o' ~, y
<br>
$ l0 m8 E; j7 Q" ]
: S: p, o2 u# D3 ~- ]+ A </form>* ~, Q- i3 W0 H- s8 x
0 h6 L- V4 c2 {& A' x <p id="demoInput"></p>
9 u9 t# ~$ B" U" ?! b" B, ~: D
' b/ [% [7 \# q2 w- L8 |( g
3 k6 ?/ ?0 N/ [ J$ Y2 L4 e( i <script>
5 c; r, F D% `+ ?! j, p$ P! r function disableFunc() {
. b1 \ F( P. g document.getElementById('mySelect').disabled = true0 J7 L5 y- _6 f! V$ o
}' Y$ V+ }9 a& o* F* y: l; Z
function ableFunc() {0 J7 C+ y9 m" M
document.getElementById('mySelect').disabled = false
% _3 k- g2 T5 O; {; q$ T" T }
8 V7 m! B N1 g; A& ^- C; ?/ l/ g. Y function getValues() {- d% c }, H9 h
var text = '<ul>'; Q9 r' Q+ j; `! d, @
var x = document.getElementById('mySelect')
& M h- ^& L7 G8 K- P for (var i = 0; i < x.length; i++) {
4 Z* f) s% W. l: j) c& s4 w text += '<li>' + x.text + '</li>'
7 l4 h0 |8 b: q! O! _ }
) Y0 Z) g1 Q8 R text += '</ul>'
0 n U# y+ f t. [) N) o1 |& T. A document.getElementById('demoInput').innerHTML = text
: [! Z* \5 c$ e( b/ u& V }
1 T. v5 Y* |% o. [# d4 e( W! v" i; m' {) b
function editValue() {; k4 j* O; Y9 b# P8 c# C
var mySelected = document.getElementById('mySelect')6 ?7 L: D9 w4 p$ D3 P; S
var selectedIndex = mySelected.selectedIndex
! A, V5 p% I5 G' K3 a G+ _: A mySelected[selectedIndex].text = document.getElementById('inputChange').value$ c3 O3 J$ B0 K; F
}
" K+ h l% l5 M/ R! Q) U) [
& }% }9 \8 `% j- l9 B function removeValue() {( \7 C8 k$ S9 o- J7 p# z1 Q
var myselect = document.getElementById('mySelect')
% J0 a" K$ }* v- ^8 g var selectedIndex = myselect.selectedIndex
0 q0 H* y; R, B& z: ^ myselect.remove(selectedIndex)
1 w8 W0 H, P- B6 ~0 B/ P. G } `; A }8 I# A5 U
function addValue() {
) a$ d; F1 A7 P# L var myselect = document.getElementById('mySelect')5 m9 @7 f$ I- h, l/ O k
var addvalue = document.getElementById('inputChange').value
! p/ `+ t! H' v5 Z( ~& F myselect.options.add(new Option(addvalue,"value"))) D2 z6 Q1 r' A# u1 [
}6 r( m, c' e" `2 j, I9 ^
</script>
2 `! t3 |; q* i+ b4 O7 X& @% r$ p</body>+ x: U% u# q( L' ^
5 K+ W" A% ~ [: \$ H+ v" Z- f8 s</html>[/mw_shl_code]
$ _$ S L6 X' y+ F) T# P5 A |
|