0
votes

Comment obtenir des données de formulaire au format JSON en utilisant JavaScript ou JQuery

Comment obtenir des données de formulaire au format JSON, je peux faire quelque chose, mais je n'ai pas reçu de valeurs de champs. Im utilise JavaScript pour obtenir des valeurs de formulaire.

Mon code complet sur ici .. S'il vous plaît visitez mon violon. P>

Full Fiddle ici .. strong> p>

Je veux obtenir toutes les valeurs telles que le format JSON P> blockQquote>

exemple p> xxx pré>

mais je peux faire quelque chose ici p>

p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


0 commentaires

3 Réponses :



0
votes

Ceci sera utile

écrire ci-dessous le code dans la fonction de soumission p>

p>

var formData = {};

$("#contactForm").find('.form-control').each(function(index, control) {



  if (this.tagName.toLowerCase() == 'select' || this.tagName.toLowerCase() == 'input') {
        formData[this.name] = $(this).val()
		
  }
});

console.log(formData)


2 commentaires

monsieur, il ne faut pas obtenir de données de table


var formdata = {}; $ ("# tab_logic"). Trouver ('TR'). Chacune (fonction (index, ligne) {var Tametow $ $ = $ (rangée); si (Tablerow $.find ("Form-Control"). Longueur> 0) {formdata ['Row -' + index] = {}; Tablerow $ .Find ('. Formulaire-contrôle'). CHAQUE (FONCTION (IND, CONTROL) {Si (this.TOname.Tolowercase () == ' Sélectionnez '|| this.tagname.tolowerCase () ==' Entrée ') {FormData [' Row - '+ Index] [Ce nom] = $ (ceci) .val ();}});}});



0
votes

Cette approche peut aider

function loadValues() {
  //move on this
  var mainArr = [];
  var tmpArr = [];
  var mainTable = $('#tab_logic');
  var tr = mainTable.find('tbody tr');
  console.log(tr.length)
  tr.each(function() {
    tmpArr = [];
    $(this).find('td').each(function() {

      //get attr id
      let title = $(this).find('input, selet').attr('id');

      var values = $(this).find('input, select').val();

      //input json format
      let pushing = $.parseJSON( '{ "' + title + '": "' + values + '" }' );

      tmpArr.push(pushing);

    });
    mainArr.push(tmpArr);
  });
  console.log(mainArr);
}


0 commentaires