Jquery create object containing form data

Asked

Viewed 640 times

0

Hello I need a help , I’m trying to create an object containing all the data of a question/answer form. in php I create the radios so :

<input type="radio" name="'.$row['id'].'" class="'.$row['id'].'" value="'.$row2['id'].'">&nbsp;'.utf8_encode($row2['resposta']).'<br>';

inserir a descrição da imagem aqui

now I need to get all these inputs and their values , detail : the table that will receive the result is like this:

id|id_usuario|id_pesquisa|id_var|id_pergunta|id_resposta.

I’d appreciate it if someone could shed some light!!!

1 answer

1


You can create an array with an example key and associated value:

var pesquisaArray = { 
id: $('input[name="id"]:checked').val(), // pegar valor de um radiobox
id_usuario: 200, 
id_pesquisa: $('#id_pesquisa').val() // valor com id id_pesquisa
//etc// 
};

you can modify the value of a key as follows:

pesquisaArray.id = 20;

or

pesquisaArray["id"] = 20;

to loop the entire array you can use the function for in

example:

for (var chave in pesquisaArray) {
  console.log("chave " + key + " tem valor  " + pesquisaArray[chave]);
}
  • 13 dev, thanks for the feedback, that helped a lot!! Another question, my Names tags comes from id related to bank question. As with each imported questionnaire there will be new id s, the same will always be different. there is some way to collect the value of the input id without knowing its id? ex: $('#1'). val(); I know this id I want to access is as one, but if I didn’t know the id, how would I get it ?

  • yes, you can, by the tag name: consulta este link https://stackoverflow.com/a/11945825/5244258, anything if it helps you can help you ^^

Browser other questions tagged

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