Alternativa para poder pasar campos readonly en el submit con jQuery

Los campos de formulario readonly no se envian en el submit del form.
El siguiente código permite duplicar todos los campos select que existan en el formulario, creando un nuevo campo con el mismo contenido, y readonly. El script oculta el campo original.
De esta manera, visualmente aparece el readonly, pero en el formulario, viajara solo el oculto.

function soloLectura(selectElementId)
{
 var selectElement = document.getElementById(selectElementId);
 if (selectElement){  
  var parent = selectElement.parentElement;
  var textValue = selectElement.options[selectElement.options.selectedIndex].innerText;
  
  if (!parent){
   parent=selectElement.parentNode;
   textValue = selectElement.options[selectElement.options.selectedIndex].text;
  }

  ip = document.createElement("select");
  ip.name = "prueba";
  ip.options[0] = new Option(textValue, "aaaaaaa");
  
  ip.disabled = true;
  
  parent.replaceChild(ip, selectElement);

  selectElement.style.background="#ff0000";
  parent.appendChild(selectElement);

  selectElement.style.visibility = "hidden";
 }
}


$(document).ready(function() {
 v = $("select");

 v.each(function() {  
  elem = $(this);
  if (elem.attr("onfocus"))
  {
   if (elem.attr("onfocus").toString().indexOf("SFReadOnly") >= 0)
   {
    soloLectura(elem.attr("name"));
   }
  }
 });

 
});

No hay comentarios:

Publicar un comentario