日期:2014-05-16  浏览次数:20524 次

Javascript select對象操作大全(新增、修改、删除、选中、清空、判断存在等)

[转载而已]看别人总结的不错,文章地址:http://www.58php.com/news.php?id=102

?

1.1判断select选项中 是否存在Value="paraValue"的Item???
2.2向select选项中 加入一个Item???
3.3从select选项中 删除一个Item???
4.4删除select中选中的项???
5.5修改select选项中 value="paraValue"的text为"paraText"???
6.6设置select中text="paraText"的第一个Item为选中???
7.7设置select中value="paraValue"的Item为选中???
8.8得到select的当前选中项的value???
9.9得到select的当前选中项的text???
10.10得到select的当前选中项的Index???
11.11清空select的项???
12.?
13.js 代码???
14.// 1.判断select选项中 是否存在Value="paraValue"的Item??????????
15.function jsSelectIsExitItem(objSelect, objItemValue) {??????????
16.??? var isExit = false;??????????
17.??? for (var i = 0; i < objSelect.options.length; i++) {??????????
18.??????? if (objSelect.options[i].value == objItemValue) {??????????
19.??????????? isExit = true;??????????
20.??????????? break;??????????
21.??????? }??????????
22.??? }??????????
23.??? return isExit;??????????
24.}???????????
25.?????
26.// 2.向select选项中 加入一个Item??????????
27.function jsAddItemToSelect(objSelect, objItemText, objItemValue) {??????????
28.??? //判断是否存在??????????
29.??? if (jsSelectIsExitItem(objSelect, objItemValue)) {??????????
30.??????? alert("该Item的Value值已经存在");??????????
31.??? } else {??????????
32.??????? var varItem = new Option(objItemText, objItemValue);????????
33.??????? objSelect.options.add(varItem);???????
34.??????? alert("成功加入");???????
35.??? }??????????
36.}??????????
37.?????
38.// 3.从select选项中 删除一个Item??????????
39.function jsRemoveItemFromSelect(objSelect, objItemValue) {??????????
40.??? //判断是否存在??????????
41.??? if (jsSelectIsExitItem(objSelect, objItemValue)) {??????????
42.??????? for (var i = 0; i < objSelect.options.length; i++) {??????????
43.??????????? if (objSelect.options[i].value == objItemValue) {??????????
44.??????????????? objSelect.options.remove(i);??????????
45.??????????????? break;??????????
46.??????????? }??????????
47.??????? }??????????
48.??????? alert("成功删除");??????????
49.??? } else {??????????
50.??????? alert("该select中 不存在该项");??????????
51.??? }??????????
52.}??????
53.?????
54.?????
55.// 4.删除select中选中的项??????
56.function jsRemoveSelectedItemFromSelect(objSelect) {??????????
57.??? var length = objSelect.options.length - 1;??????
58.??? for(var i = length; i >= 0; i--){??????
59.??????? if(objSelect[i].selected == true){??????
60.??????????? objSelect.options[i] = null;??????
61.??????? }??????
62.??? }??????
63.}????????
64.?????
65.// 5.修改select选项中 value="paraValue"的text为"paraText"??????????
66.function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {??????????
67.??? //判断是否存在??????????
68.??? if (jsSelectIsExitItem(objSelect, objItemValue)) {??????????
69.??????? for (var i = 0; i < objSelect.options.length; i++) {??????????
70.??????????? if (objSelect.options[i].value == objItemValue) {??????????
71.??????????????? objSelect.options[i].text = objItemText;??????????
72.??????????????? break;??????????
73.??????????? }??????????
74.??????? }??????????
75.??????? alert("成功修改");??????????
76.??? } else {??????????
77.??????? alert("该select中 不存在该项");??????????
78.??? }??????????
79.}??????????
80.?????
81.// 6.设置select中text="paraText"的第一个Item为选中??????????
82.function jsSelectItemByValue(objSelect, objItemText) {??????????????
83.??? //判断是否存在??????????
84.??? var isExit = false;??????????
85.??? for (var i = 0; i < objSelect.options.length; i++) {??????????
86.??????? if (objSelect.options[i].text == objItemText) {??????????
87.??????????? objSelect.options[i].selected = true;??????????
88.??????????? isExit = true;??????????
89.??????????? break;??????????
90