javascript²Ù×÷selectÏà¹Ø·½·¨£ºÐÂÔö ÐÞ¸Ä É¾³ý Ñ¡ÖÐ Çå¿Õ ÅжϴæÔÚ
Javascript ²Ù×÷selectÊÇ±íµ¥Öг£¼ûµÄÒ»ÖÖ£¬½ñÌìɾ³ý¶à¸öselectÖµµÄʱºò³öÏÖÁËÎÊÌ⣬¸ãÁË°ëÌìÔÀ´ÊÇË÷ÒýÒýÆðµÄ£¨¼´É¾³ýµÄʱºòÒª´ÓË÷Òý´óµÄ¿ªÊ¼É¾£¬È»ºóÔ٠ɾ³ýË÷ÒýСµÄ£¬·ñÔòɾ³ýÁËË÷ÒýСµÄºóË÷Òý´óµÄË÷Òý¾Í±ä»¯ÁË£¬ÔÙɾ³ýʱ¾Í»á³öÏÖÎÊÌ⣣ÎÊÌâµÄ¹Ø¼üÊÇforÑ»·ÊÇÒª´Ó´óµ½Ð¡£¬¶ø²»Êdz£¹æµÄ´Ó0µ½ length£©
// 4.ɾ³ýselectÖÐÑ¡ÖеÄÏî
function jsRemoveSelectedItemFromSelect(objSelect) {
var length = objSelect.options.length - 1;
for(var i = length; i >= 0; i--){
if(objSelect[i].selected == true){
objSelect.options[i] = null;
}
}
}
1ÅжÏselectÑ¡ÏîÖÐ ÊÇ·ñ´æÔÚValue="paraValue"µÄItem
2ÏòselectÑ¡ÏîÖÐ ¼ÓÈëÒ»¸öItem
3´ÓselectÑ¡ÏîÖРɾ³ýÒ»¸öItem
4ɾ³ýselectÖÐÑ¡ÖеÄÏî
5ÐÞ¸ÄselectÑ¡ÏîÖÐ value="paraValue"µÄtextΪ"paraText"
6ÉèÖÃselectÖÐtext="paraText"µÄµÚÒ»¸öItemΪѡÖÐ
7ÉèÖÃselectÖÐvalue="paraValue"µÄItemΪѡÖÐ
8µÃµ½selectµÄµ±Ç°Ñ¡ÖÐÏîµÄvalue
9µÃµ½selectµÄµ±Ç°Ñ¡ÖÐÏîµÄtext
10µÃµ½selectµÄµ±Ç°Ñ¡ÖÐÏîµÄIndex
11Çå¿ÕselectµÄÏî
======================================================================
¶¯Ì¬É¾³ýselectÖеÄËùÓÐoptions£º
function deleteAllOptions(sel){
sel.options.length=0;
}
¶¯Ì¬É¾³ýselectÖеÄijһÏîoption£º
function deleteOption(sel,indx){
sel.options.remove(indx);
}
¶¯Ì¬Ìí¼ÓselectÖеÄÏîoption:
function addOption(sel,text,value){
sel.options.add(new Option(text,value));
}
ÉÏÃæÔÚIEºÍFireFox¶¼ÄܲâÊԳɹ¦£¬Ï£ÍûÒÔºó¿ÉÒÔÓÃÉÏ¡£
===========================================
js ´úÂë
// 1.ÅжÏselectÑ¡ÏîÖÐ ÊÇ·ñ´æÔÚValue="paraValue"µÄItem
function jsSelectIsExitItem(objSelect, objItemValue) {
var isExit = false;
for (var i = 0; i < objSelect.options.length; i++) {
if (objSelect.options[i].value == objItemValue) {
isExit = true;
break;
}
}
return isExit;
}
// 2.ÏòselectÑ¡ÏîÖÐ ¼ÓÈëÒ»¸öItem
function jsAddItemToSelect(objSelect, objItemText, objItemValue) {
//ÅжÏÊÇ·ñ´æÔÚ
if (jsSelectIsExitItem(objSelect, objItemValue)) {
alert("¸ÃItemµÄValueÖµÒѾ´æÔÚ");
} else {
var varItem = new Option(objItemText, objItemValue);
objSelect.options.add(varItem);
alert("³É¹¦¼ÓÈë");
}
}
// 3.´ÓselectÑ¡ÏîÖРɾ³ýÒ»¸öItem
function jsRemoveItemFromSelect(objSelect, objItemValue) {
//ÅжÏÊÇ·ñ´æÔÚ
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < objSelect.options.length; i++) {
if (objSelect.options[i].value == objItemValue) {
objSelect.options.remove(i);
break;
}
}
alert("³É¹¦É¾³ý");
} else {
alert("¸ÃselectÖÐ ²»´æÔÚ¸ÃÏî");
}
}
// 4.ɾ³ýselectÖÐÑ¡ÖеÄÏî
function jsRemoveSelectedItemFromSelect(objSelect) {
var length = objSelect.options.length - 1;
for(var i = length; i >= 0; i--){
if(objSelect[i].selected == true){
objSelect.options[i] = null;
}
}
}
// 5.ÐÞ¸ÄselectÑ¡ÏîÖÐ value="paraValue"µÄtextΪ"paraText"
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {
//ÅжÏÊÇ·ñ´æÔÚ
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < objSelect.options.length; i++) {
if (objSelect.options[i].value == objItemValue) {
objSelect.options[i].text = objItemText;
break;
}
}
alert("³É¹¦ÐÞ¸Ä");
} else {
alert("¸ÃselectÖÐ ²»´æÔÚ¸ÃÏî");
}
}
// 6.ÉèÖÃselectÖÐtext="paraText"µÄµÚÒ»¸öItemΪѡÖÐ
function jsSelectItemByValue(objSelect, objItemText) {
//ÅжÏÊÇ·ñ´æÔÚ
var isExit = false;
for (var i = 0; i < objSelect.options.length; i++) {
if (objSelect.options[i].text == objItemText) {
objSelect.options[i].selected = true;
isExit = true;
break;
}
}
//Show³ö½á¹û
if (isExit) {
alert("³É¹¦Ñ¡ÖÐ");
} else {
alert("¸ÃselectÖÐ ²»´æÔÚ¸ÃÏî");
}
}
// 7.ÉèÖÃselectÖÐvalue="paraValue"µÄItemΪѡÖÐ
objSelect.value = objItemValue;
// 8.µÃµ½selectµÄµ±Ç°Ñ¡ÖÐÏîµÄvalue
var currSelectValue = objSelect.value;
// 9.µÃµ½selectµÄµ±Ç°Ñ¡ÖÐÏîµÄtext
var currSelectText = objSelect.options[document.all.objSelect.selectedIndex].text;
// 10.µÃµ½selectµÄµ±Ç°Ñ¡ÖÐÏîµÄIndex
var currSelectIndex = objSelect.selectedIndex;
// 11.Çå¿ÕselectµÄÏî
objSelect.options.length = 0;
Õû¸öʵÀýµÄÍêÕû´úÂëÈçÏ£º
<html>
<head>
<title>javascript select options text value</title>
<meta name="keywords" content="javascript select options text value add modify delete set">
<meta name="description" content="javascript select options text value add modify delete set">
<script language="javascript">
<!--
// Author: i@lxl.cn
// Modify: i@cnlei.com
function watch_ini(){ // ³õʼ
for(var i=0; i<arguments.length; i++){
var oOption=new Option(arguments[i],arguments[i]);
document.getElementById("MySelect")[i]=oOption;
}
}
function watch_add(f){ // Ôö¼Ó
var oOption=new Option(f.word.value,f.word.value);
f.keywords[f.keywords.length]=oOption;
}
function watch_sel(f){ // ±à¼
f.word.value = f.keywords[f.keywords.selectedIndex].text;
}
function watch_mod(f){ // ÐÞ¸Ä
f.keywords[f.keywords.selectedIndex].text = f.word.value;
}
function watch_del(f){ // ɾ³ý
f.keywords.remove(f.keywords.selectedIndex);
}
function watch_set(f){ // ±£´æ
var set = "";
for(var i=0; i<f.keywords.length; i++){
set += f.keywords[i].text + ";";
}
confirm(set);
}
//-->
</script>
</head>
<body>
<form name="watch" method="post" action="">
<select id="MySelect" name="keywords" size="10" onchange="watch_sel(this.form)"></select><br>
<script language="javascript">
<!--
watch_ini("ÎÒ","Äã","Š…","Ëû","Ëý","Ëü","¶û"); // ³õʼ¹Ø¼ü´Ê
//-->
</script>
<input type="text" name="word" /><br />
<input type="button" value="Ôö¼Ó" onclick="watch_add(this.form);" />
<input type="button" value="ÐÞ¸Ä" onclick="watch_mod(this.form);" />
<input type="button" value="ɾ³ý" onclick="watch_del(this.form);" />
<input type="button" value="±£´æ" onclick="watch_set(this.form);" />
</form>
</body>
</html>
ÍƼöÐÅÏ¢
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- jQuery Tooltips²å¼þ
- jQueryµÄ.bind()¡¢.live()ºÍ.delegate(....
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- Ä㲻ϲ»¶»ðºü£¬µ«ÓÖϲ»¶firebug£¬ÄǾ͸úÎÒÒ»ÆðÀ´ÔÚIEÉÏ....
- ¸ù¾ÝÏÔʾÆ÷·Ö±æÂʵ÷ÕûÒ³ÃæÏÔʾµÄJavascript½Å±¾
- jQuery дµÄ·ÂÐÂÀË΢²© ÏòϹö¶¯Ð§¹û¡¾×ªÔØ¡¿
- ͨÓõÄJqueryÑ¡ÔñÆ÷
- jQuery ½áºÏ Json Ìá½»Êý¾Ýµ½Webservice£¬....
- ×ÔÖÆjQueryÖÇÄÜÌáʾ²å¼þһö
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- js»ñÈ¡srcÖд«µÝµÄ²ÎÊý
- jqueryʵÏÖÍøվͼƬÀÁ¼ÓÔØ´úÂë
- ÈÃä¯ÀÀÆ÷ÆÁ±ÎµôJavaScriptµÄ³ö´íÌáʾ
- github²»ÄܼÓÔØcss¡¢js½â¾ö°ì·¨
- ÎÒÖªµÀµÄJavaScript -- Éè¼Æģʽ(ÇŽÓ)Ó¦ÓÃÖ® ¨C ....
- 15¸öÖµµÃ¿ª·¢ÈËÔ±¹Ø×¢µÄjQuery¿ª·¢¼¼ÇɺÍÐĵÃ
- dz̸JavascriptÃæÏò¶ÔÏó±à³Ì
- JS¼òµ¥¶¯»·â×°
- JavaScript³õѧÕßӦעÒâµÄÆ߸öϸ½Ú
- jQuery Tooltips²å¼þ
ÆÀÂÛ