Text Resizer Text Resizer
September 8th, 2010
You're browsing: Scriptmatico.Com » General, Javascript » “Select All” Seleccionando todos los elementos que se postean como un array

“Select All” Seleccionando todos los elementos que se postean como un array

Posted on Abr 18 in General, Javascriptby adminPrintText Resizer Text Resizer

Esta es una pequeña traduccion de un articulo que me encontre y se me hizo buena idea traducirlo y ponerlo a la dispocicion de todos.

El articulo se llama "Select All" JavaScript for Forms Posting to an Array

Bien este articulo nos explica lo siguiente:

El problema original es que al enviar nuestro formulario a un script PHP, con el fin de tener el resultado de una matriz de controles (cuando se utiliza una serie de casillas de verificación, por ejemplo), la forma más rápida de hacer esto es añadir "[]" para el nombre del elemento ( Por ejemplo 'name = "zona[]"). Luego, PHP puede acceder a $ _POST [ 'zona'] como una matriz en el script.

Pero lamentablemente, la adición de corchetes causas problemas con JavaScript, especialmente con una función "Seleccionar todo". El siguiente script funciona en torno a el uso de expresiones regulares, y puede ser útil a alguien que experimente este mismo problema más adelante.

HTML:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <title>Checkbox Fun</title>
  3. <script type="text/javascript"><!--
  4. var formblock;
  5. var forminputs;
  6. function prepare() {
  7.   formblock= document.getElementById('form_id');
  8.   forminputs = formblock.getElementsByTagName('input');
  9. }
  10. function select_all(name, value) {
  11.   for (i = 0; i <forminputs.length; i++) {
  12.     // regex here to check name attribute
  13.     var regex = new RegExp(name, "i");
  14.     if (regex.test(forminputs[i].getAttribute('name'))) {
  15.       if (value == '1') {
  16.         forminputs[i].checked = true;
  17.       } else {
  18.         forminputs[i].checked = false;
  19.   }
  20.     }
  21.   }
  22. }
  23. if (window.addEventListener) {
  24.   window.addEventListener("load", prepare, false);
  25. } else if (window.attachEvent) {
  26.   window.attachEvent("onload", prepare)
  27. } else if (document.getElementById) {
  28.   window.onload = prepare;
  29. }
  30. //--></script>
  31. </head>
  32.  
  33.  
  34. <form id="form_id" name="myform" method="get" action="search.php">
  35.  
  36.   <a href="#" onClick="select_all('area', '1');">Check All Fruit</a> | <a href="#" onClick="select_all('area', '0');">Uncheck All
  37. Fruit</a><br /><br />
  38.  
  39.   <input type="checkbox" name="area[]" value="1" />Apples<br />
  40.   <input type="checkbox" name="area[]" value="2" />Bananas<br />
  41.   <input type="checkbox" name="area[]" value="3" />Chickens<br />
  42.   <input type="checkbox" name="area[]" value="4" />Stoats
  43.  
  44.   <br /><br /><a href="#" onClick="select_all('location', '1');">Check All Locations</a> | <a href="#" onClick="select_all('location',
  45. '0');">Uncheck All Locations</a><br /><br />
  46.  
  47.   <input type="checkbox" name="location[]" value="1" />Brighton<br />
  48.   <input type="checkbox" name="location[]" value="2" />Hove<br />
  49.  
  50. </form>
  51.  
  52. </body>
  53. </html>

Post to Twitter


  • No Related Post

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Back to Top
[x] Cerrar
E-mail