document.getElementById ("Louyuming"). Opciones [0] .Selected = True; Function JSSelecteSexitITem (objSelect, objitemValue) {var isExit = false; for (var i = 0; i <objSelect.options.length; i ++) {if (objSelect.options [i] .Value == objitemValue) {isExit = true; romper; }} return isExit; }JavaScript Operation Select es un tipo común en formularios. Hoy, al eliminar múltiples valores seleccionados, había un problema. Después de mucho tiempo, resultó que fue causado por el índice (es decir, cuando se elimina, debe comenzar desde el índice grande y luego eliminar el índice pequeño, de lo contrario el índice con el índice grande cambiará después de eliminar el índice pequeño, y luego el índice grande cambiará. Habrá problemas al eliminar nuevamente; la clave para el problema es que el bucle debe ir de gran a pequeño a pequeño, en lugar de la longitud de 0 a la regular) a la longitud))).
// 4. Elimine el elemento seleccionado en la función seleccionar JSRemoVesElectedIditEMFromSelect (objSelect) {var longitud = objselect.options.length - 1; for (var i = longitud; i> = 0; i-) {if (objSelect [i] .selected == true) {objSelect.options [i] = null; }}}1 Determinar si hay un elemento con value = "paraValue" en la opción Seleccionar
2 Agregue un elemento a la opción Seleccionar
3Remove un elemento de la opción Seleccionar
4 Eliminar el elemento seleccionado en Seleccionar
5 Modifique el texto de value = "ParaValue" en la opción Seleccionar a "Paratext"
6Set el primer elemento de texto = "Paratext" en Seleccionar según lo seleccionado
7 Establezca el elemento de valor = "paraValue" en seleccionar según lo seleccionado
8 Obtenga el valor del artículo seleccionado actualmente
9 Obtenga el texto del elemento seleccionado actualmente
10 Obtenga el índice del elemento seleccionado actualmente
11 Borrar el elemento seleccionado
============================================================================================================================================================== ¡
Eliminar dinámicamente todas las opciones en Seleccionar:
function DeleteAlLOptions (SEL) {Sel.options.length = 0; }Eliminar dinámicamente una opción en Seleccionar:
function deleteOption (sel, indx) {sel.options.remove (indx); }Opción de elemento agregue dinámicamente en Seleccionar:
function addOption (sel, text, valor) {sel.options.add (nueva opción (texto, valor)); }Las pruebas anteriores se probaron con éxito en IE y Firefox, y espero que puedan usarse en el futuro.
=============================================================
código JS
// 1. Determine si hay una función de elemento de value = "paraValue" en la opción Seleccionar JSSelectIeSexititem (objSelect, objitemValue) {var isExit = false; for (var i = 0; i <objSelect.options.length; i ++) {if (objSelect.options [i] .Value == objitemValue) {isExit = true; romper; }} return isExit; } // 2. Agregue una función de elemento a la opción Seleccionar jsadditemToselect (objelect, objitemText, objitemValue) {// juzga si if (jsselectexititem (objelect, objitemValue)) {alerta ("El valor de este ítem ya existe"); } else {var varitem = nueva opción (objitemtext, objitemValue); objelect.options.Add (varitem); alerta ("unido con éxito"); }} // 3. Elimine una función de elemento de la opción Seleccionar jsremoveItemFromSelect (objSelect, objitemValue) {// juzga si hay if (jsselectexititem (objelect, objitemValue)) {for (var i = 0; i <objelect.options.length; i ++) {si (objSelect.options [i] .value == objitemValue) {objSelect.options.remove (i); romper; }} alert ("eliminado con éxito"); } else {alerta ("Este elemento no existe en esta selección"); }} // 4. Elimine el elemento seleccionado en la función seleccionar jsremovesElecteditemFromSelect (objSelect) {var longitud = objSelect.options.length - 1; for (var i = longitud; i> = 0; i-) {if (objSelect [i] .selected == true) {objSelect.options [i] = null; }}} // 5. Modifique el texto de value = "paraValue" en la opción Seleccionar a la función "Paratext" JSUPDateItemToSelect (objSelect, objitemtext, objitemValue) {// juzga si hay si hay (jselectexitem (objelect, objitemvalue) {for (var i = 0; objelect.options.length; romper; }} alert ("modificado correctamente"); } else {alerta ("Este elemento no existe en esta selección"); }} // 6. Establezca el primer elemento de texto = "Paratext" en selección como función seleccionada JSSelectitEmByValue (objSelect, objitemtext) {// juzga si 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; romper; }} // Mostrar resultado if (isExit) {alert ("seleccionado correctamente"); } else {alerta ("Este elemento no existe en esta selección"); }} // 7. Establezca el elemento de valor = "Paravalue" en la selección de ser seleccionado objSelect.Value = objitemValue; // 8. Obtenga el valor var del elemento seleccionado actualmente de select CurrSelectValue = objSelect.Value; // 9. Obtenga el texto var currSelectText del elemento seleccionado actualmente de select var currselectText = objselect.options [document.all.objselect.selectedIndex] .Text; // 10. Obtenga el índice del elemento seleccionado actualmente de select var currselectindex = objselect.selectedIndex; // 11. Borre el elemento de seleccionar objelect.options.length = 0;El código completo de toda la instancia es el siguiente:
<! Doctype html public "-// w3c // dtd html 4.0 transitional // en"> <html> <fead> <title> javascript Seleccione Opciones Valor de texto </title> <meta name = "Palabras de clave" Content = "Javascript Seleccionar Valor de texto de texto Agregar ADD Modify Set"> <Meta Name = "Content" Content = "JavaScriptscriptscripts" Content de JavaScript Seleccione Opciones Seleccionar Valor de texto Agregar ADDETETEETE Set "> <Meta Name =" Descripción "Content =" JavaScriptscriptscriptscripts " establecer "> <script language =" javascript "> <!-// autor: [email protected]// modificar: [email protected] watch_ini () {// inicial para (var i = 0; i <arguments.length; i ++) {var ooption = nuevo = nuevo Opción (argumentos [i], argumentos [i]); document.getElementById ("mySelect") [i] = ooption;}} function watch_add (f) {// agregue var ooption = new option (f.word.value, f.word.value); f.keywords [f.keywordwords.length] = ooption;} watch_sel (f) {F) f.word.value = f.keywords [f.keywords.selectedIndex] .Text;} function watch_mod (f) {// modificar f.keywords [f.keywordwords.selectedIndex] .Text = f.word.value;} function watch_del (f) {// delete f.keywords.remove (f.keywords.nectedIndex); watch_set (f) {// save var set = ""; for (var i = 0; i <f.keywords.length; i ++) {set += f.keywords [i] .Text +";";} confirmar (set);} //-> </script> </head> <body> <form de nombre = "watch" método = "post" action = ""> <select id = "myselect" name = "keyords" size = "10" onchange = "watch_sel (this.form) lenguaje = "javascript"> <!-watch_ini ("i", "you", "él", "ella", "ella", "it", "el"); // Palabras clave iniciales //-> </script> <input type = "text" name = "word"/> <br/> <input type = "button" value = "add" onClick = "watch_add (this.form);" /> <input type = "button" value = "modify" onClick = "watch_mod (this.form);" /> <input type = "button" value = "delete" onClick = "watch_del (this.form);" /> <input type = "button" value = "save" onClick = "watch_set (this.form);" /> </form> </body> </html>