Catch value input radio or input checkbox

0

I have a database query that takes out several radios and checkbox and I want to take the value and save it in an array in jquery by clicking a button. Could it be done without taking the name? I have an question with several answers and each question is in a table and when you ask all questions there is an input button

    
asked by bsg 22.05.2017 в 09:52
source

1 answer

2

You do not need jQuery for this job. You can use FormData to do your work and then FormData.entries() to iterate through all the fields in the form:

function comprobar(obj) {
  var formulario = new FormData(obj);
  document.getElementById('salida').innerText = '';
  /* Recorremos todos los valores (entries) */
  for(var dupla of formulario.entries()) {
    document.getElementById('salida').innerText += dupla[0]+ ': '+ dupla[1] + "\n";
  }
  return false;
}
<pre id="salida"></pre>
<form onsubmit="return comprobar(this)">
  <input type="hidden" name="oculto" value="valor oculto" /><br />
  <input type="text" name="texto" value="valor de texto" /><br />
  <input type="password" name="contraseña" value="valor de contraseña" /><br />
  <textarea name="areadetexto">Contenido del área de texto</textarea><br />
  <select name="colores" multiple>
    <option value="rojo" selected>Color Rojo</option>
    <option value="azul">Color Azul</option>
    <option value="negro" selected>Color Negro</option>
  </select><br />
  <input type="radio" name="modelo" value="nuevo" checked> Coche nuevo<br />
  <input type="radio" name="modelo" value="viejo"> Coche viejo<br />
  <input type="submit">
</form>
    
answered by 22.05.2017 / 10:06
source