|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 ~7 E- n, _: p7 `9 l( X# c& q/ I( R
JS 操作 Select相关功能测试
0 |; _$ M) o8 u8 O- K! k# G
" p- K+ T! P' \ D/ Z$ f[mw_shl_code=html,true]<html lang="en">
4 X Z5 g% C4 m/ r+ x
5 U/ F( O8 C R- l4 j2 o<head>
4 J" h/ b1 s3 r1 Q- c$ W <meta charset="UTF-8">
( j% f( T8 W+ p5 B; F+ l* m <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 r, z2 h# I6 C9 z( X- a1 x <meta http-equiv="X-UA-Compatible" content="ie=edge">0 ?. V# f& g, [" X) a K% {' a9 e0 f" g
<title>Document</title>4 q5 m! K/ K; v) l A9 e f
<style>* F7 Q" F( G- i
* {3 F4 ]% Q# F {; D# k8 C1 J
font-size: 10px;
( Q( W; w- H) V+ G9 }3 p4 i# U8 d [ line-height: 24px;
3 z1 P# _8 q% n* f8 u, s4 ^ }
0 ~8 F1 D6 r. V, Z0 Y </style>
+ m1 T* r! n" j: H/ e4 \</head>
5 _( N9 V, k( M; L; E9 y
1 T9 ]6 V% G O3 u8 U5 I' I<body>
: C3 ~5 s1 A" _+ i$ ]4 ~" Q //this is for the select test
! i1 z" c8 L6 N8 h( \/ O; l/ g <form id='myForm' action="submit" method="GET">4 i$ C# T8 _* o5 {! p7 U
<input type="text" id="inpuTChange" />
9 b3 _. c& |5 I; x* E. l <select name="selectFruits" id="mySelect">8 ^3 r9 z$ F$ P) {* o9 ^! E
<option value="Orange1">Orange</option>2 h6 Y# h, K* [* r6 O ?9 J6 r
<option value="Apple2">Apple</option>
I: X7 v5 x) @7 W5 p <option value="Banana3">Banana</option>
: J" v( y: E, o6 S& ]( y </select>. s! X. x+ @* r8 Y' z% G
<br>% d4 b; F2 O1 @
<input type="button" value="disable">
7 y, {! `* g* X- N; @4 L- W <input type="button" value="Able">+ y2 j* p' D J
<input type="button" value="getValues">- [6 }: C' @7 Z
<input type="button" value="editValues">
1 I, c6 ~( p5 ] R8 }/ @ <input type="button" value="addValues">
: N: ^7 }! u) j& Y, T$ H <input type="button" value="removeValues">
: p! r; D3 O- o, J* B+ l$ s/ y p* ? <br>
) k! o* R+ c( ]- B7 H7 j3 J0 a. E3 [0 y1 L$ N3 j+ n. N
</form>
P2 l2 n7 p+ e! T! s$ d% w1 c B
% F' I& t# d3 t9 w$ C# X <p id="demoInput"></p>5 ?4 N: ^4 H$ k+ K1 m" T8 }
1 J) [! g2 b, [) q& m) Q$ G
# z5 ?; ]+ b# o; Y2 Y9 y1 F <script>
! _1 `# c/ U2 j" j# x( ?0 q0 t function disableFunc() {
2 A% T# P$ T" E1 \$ u0 ^9 Z1 |( } document.getElementById('mySelect').disabled = true
# l( G5 N6 R1 N, B, y }
; G2 v" K$ {. e/ S0 T function ableFunc() {. C. j0 S" }, b: R5 G
document.getElementById('mySelect').disabled = false1 a5 `* H" p) }" K: L i
}, c/ L4 E" R! X2 T. i* @: `7 w7 b/ \* W
function getValues() {
0 l7 J# q: [5 ^- G" w8 U8 l% x var text = '<ul>': f% e: {8 R# H
var x = document.getElementById('mySelect')/ o1 C" S7 c) b8 W5 N5 ]0 i5 e( S6 R+ g
for (var i = 0; i < x.length; i++) {
5 E c% T9 [4 B" ^' M/ P text += '<li>' + x.text + '</li>'8 D! g. m7 G+ [! @& V
}
4 h5 }7 b( U5 @/ A text += '</ul>'
6 |6 A. N$ A+ O, w: A document.getElementById('demoInput').innerHTML = text+ R5 k7 {$ h) P) F1 `- g
}' |- t& Z% f1 c. q" p
f+ Z" Z& Z( ~$ i5 C function editValue() {
O- H# i* f9 {1 e var mySelected = document.getElementById('mySelect')
7 o/ L2 k& ^. v6 K, T% e" \8 U, L var selectedIndex = mySelected.selectedIndex
. d2 s- q" @0 P/ J2 } mySelected[selectedIndex].text = document.getElementById('inputChange').value
- K( o; G0 W& R* x. v' s' Z }9 b! f8 a- g# j+ G, Q" R/ U
* Y+ d9 d% v3 J3 V% R& J function removeValue() {) \& ^* d6 ^5 N; [& X; s
var myselect = document.getElementById('mySelect')
& x- D' Q+ D+ S7 y4 `$ t var selectedIndex = myselect.selectedIndex8 H! `" a( Q5 t% e, D6 m! t& j( d* Z
myselect.remove(selectedIndex)
; `% R- G: ^7 }' L) C }. E! w* ?, V+ q, j! |" D
function addValue() {
+ ? O3 _- q' D var myselect = document.getElementById('mySelect'); O' p7 ?8 x) [- T0 ^9 O e
var addvalue = document.getElementById('inputChange').value- p; B \' N; y" b) H* o" m) ^
myselect.options.add(new Option(addvalue,"value"))
7 m/ e5 l% ~/ k8 Y9 P: B } F- T$ O& _! |' z
</script>; R( h7 \3 a" D5 @6 f% C0 m2 A
</body>5 F: j9 P. h* `1 g, Y2 F* Y
# _) {& ^1 m5 ~: n: h ]& F5 m1 a
</html>[/mw_shl_code]
: ~, v1 e* C* b$ {' w% u |
|