Created
February 20, 2014 17:33
-
-
Save juanwilde/9119124 to your computer and use it in GitHub Desktop.
select¿?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Gestion de formularios 1</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="js/jscript.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<form name="form1" action="index.html"> | |
<label>Frutas favoritas</label> | |
<br /> | |
<select id="fruits" name="fruits" style="width:80px"> | |
</select> | |
<br /> | |
<label>Nombre de fruta a añadir</label> | |
<br /> | |
<input type="text" id="newFruit" /> | |
<br /> | |
<input type="radio" name="addDel" /><label>Añadir elemento</label> | |
<input type="radio" name="addDel" /><label>Borrar elemento</label> | |
<br /> | |
<input type="button" id="send" onclick="mainframe()" value="Ejecutar operación" /> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//iniciamos la ejecución una vez pulsado el botón | |
function mainframe(){ | |
//comprobamos la operación a desarrollar (añadir o borrar) | |
function checkOperation(){ | |
var selected = -1; | |
for(var i = 0; i < document.form1.addDel.length; i++) { | |
if(document.form1.addDel[i].checked){ | |
selected = i; | |
} | |
} | |
return selected; | |
} | |
//esta función recibe una cadena de texto y devuelve "false" si contiene números o "true" si no. | |
function checkNumbersInName(aString){ | |
var is = true; | |
for(var i = 0; i < aString.length; i++) | |
{ | |
for(var j = 0; j < 10; j++) | |
{ | |
if(aString.charAt(i) === j){ | |
is = false; | |
} | |
} | |
} | |
return is; | |
} | |
//dependiendo del resultado de la función anterior, operamos: -1 (ninguno seleccionado), | |
//0 (añadir) y 1 (quitar) | |
if(checkOperation() !== -1){ | |
if(checkOperation() === 0){ | |
if(checkNumbersInName(document.getElementById('newFruit'))){ | |
var x = document.getElementById('fruits'); | |
var newFruit = document.getElementById('newFruit').value.charAt(0).toUpperCase() + document.getElementById('newFruit').value.slice(1); | |
var option = document.createElement('option'); | |
option.text = newFruit; | |
x.add(option); | |
//para ordenar los elementos, los pasamos a un array, lo ordenamos | |
//y reescribimos el select | |
var fruitArray = new Array(); | |
pos = document.form1.fruits.length; | |
for(var i = 0; i < pos; i++) | |
{ | |
fruitArray[i] = document.getElementById('fruits').options[i].text; | |
} | |
fruitArray.sort(); | |
/*++++++++++++++++++++++++++++++++++ | |
código para insertar de nuevo los valores ordenados en el select | |
++++++++++++++++++++++++++++++++++*/ | |
} | |
} | |
else{ | |
document.form1.fruits.options[document.form1.fruits.selectedIndex] = null; | |
} | |
} | |
else{ | |
alert('No se ha seleccionado una operación. Inténtalo de nuevo.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment