用javascript操作select
?1
1
?檢測是否有選中
?2
if
(objSelect.selectedIndex?
>
?
-
1
)?
{
?3
//
說明選中
?4
}
?
else
?
{
?5
//
說明沒有選中
?6
}
?7
?8
2
?刪除被選中的項?
?9
objSelect.options[objSelect.selectedIndex]?
=
?
null
;
10
11
3
?增加項
12
objSelect.options[objSelect.length]?
=
?
new
?Option(
"
你好
"
,
"
hello
"
);
13
14
4
?修改所選擇中的項
15
objSelect.options[objSelect.selectedIndex]?
=
?
new
?Option(
"
你好
"
,
"
hello
"
);
16
17
5
?得到所選擇項的文本
18
objSelect.options[objSelect.selectedIndex].text;
19
20
6
?得到所選擇項的值
21
objSelect.options[objSelect.selectedIndex].value;
<
form???name
=
"
test
"
>
???
??
<
select???name
=
"
obj
"
></
select
>
???
??
<
input???type
=
"
button
"
???onclick
=
"
javascript:itemDelete(document.test.obj);
"
>
???
??
</
form
>
???
??
//
delete???
??
function
???itemDelete(obj)
{???
??
var
???lengthObj???
=
???obj.length;???

??
for
(i???
=
???
0
;i???
<
???lengthObj;i
++
)
{???

??
if
(obj.options[i].selected)
{???
??obj.remove(i);???
??lengthObj???
=
???obj.length;???
??i
--
;???
??}
???
??}
???
??}
function
???delOption()
{???
??????
var
???obj???
=
???document.myForm.aa;???
??????
var
???opts
=
???obj.options;???
??????
var
???index???
=
???
1
;???
??????opts[index]???
=
???
null
;???
??}
???

??
function
???addOption()
{???
??????
var
???obj???
=
???document.myForm.aa;???
??????
var
???opts
=
???obj.options;???
??????
var
???opt???
=
???
new
???Option(
"
五
"
,
"
五
"
);???
??????opts.length
++
;???
??????opts[opts.length
-
1
]???
=
???opt;???
??}
???

?2



?3

?4



?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21








































posted on 2007-03-05 08:30 風人園 閱讀(388) 評論(0) 編輯 收藏 所屬分類: JavaScript