How to serialize with input disable?

Asked

Viewed 645 times

4

In a reply to SOEN

They pass an approach of temporarily disabling the camps:

Example:

var myform = $('#myform');

 // Encontra os elementos e remove o atributo disable
var disabled = myform.find(':input:disabled').removeAttr('disabled');

 // serialize o form
var serialized = myform.serialize();

 // coloca todos os elementos que foram desabilidados disable
disabled.attr('disabled','disabled');

Is there any more efficient way to do serialize() in form fields that are disabled ?

  • I do not think, I believe that you will always have to remove the attribute 'disabled'.

  • 1

    This question has 2 closing votes as out of scope for not being a programming problem. Who voted for the closure, would like to justify? For to me it seems to be a question perfectly within the scope. She’s in the closing analysis queue, and since I don’t see anything wrong I’m going to click on the "Leave it open".

1 answer

1


You could solve this problem by going through the elements disabled to catch the name e o value and finally concatenating with the serialized string to send to the server.

Example:

function getDisableInput(form) {
   var input = $("#" + form + " input:disabled");
   var result = '';
   $.each(input, function (key, val) {
      result += "&" + val.name + '=' + val.value;
   });
   return result;
}

var disableInput = getDisableInput('form');  
var dados = $("#form").serialize() + disableInput; // agora é só enviar para o servidor.

Another interesting way to achieve the same result is to traverse all fields of the selector 'input[type=text]' so it won’t be necessary to use serialize for type text input.

Example 2:

var dados = '';

$('input[type=text]').each(function() {
  dados += '&' + this.id + '=' + this.value;
});

console.log(dados);
  • Read these comments here.

  • @Edilson is a requirement to be disabled and not readyonly, the response of "tvanfosson" is done in the same way as mine. What differs is that mine presents a little more performance by using id’s selectors and not using attr being that they could be accessed by . attribute directly.

  • Comments referring to '&' not to selectors, in addition to being limited to type text.

  • @Edilson would be the one from gruszczy saying he should first check if the form is empty to not send & alone ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.