Skip to main content
adicionou 704 caracteres ao conteúdo
Fonte Link
Sam
  • 80,8mil
  • 22
  • 73
  • 127

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   var $this = $(this);
   
   if(i == 0){ 

      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [{}]; 

   }else{
      
      obj[form1][0][$(this)obj[form1][0][$this.attr("id")] = [{}];

      $(".linha", $this).each(function(){
      
        [$ obj[form1][0][$this.attr("id")][0][$("[name='dia']", $(this)).val()] := [{
       
   
             "inicio" : $("[name='inicio']", $(this)).val(),
               "fim" : $("[name='fim']", $(this)).val(),
         
         }]
      }]);
   }
   
});

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
   <input type="text" name="nome" value="Fulano">
</form> 

<form id="turno1">
   <div class="linha">
      <input type="text" name="dia" value="dia1">value="t1 d1">
      <input type="text" name="inicio" value="ini1">value="t1 ini1">
      <input type="text" name="fim" value="fim1">value="t1 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t1 d2">
      <input type="text" name="inicio" value="t1 ini2">
      <input type="text" name="fim" value="t1 fim2">
   </div>
</form> 

<form id="turno2">
   <div class="linha">
      <input type="text" name="dia" value="dia2">value="t2 d1">
      <input type="text" name="inicio" value="ini2">value="t2 ini1">
      <input type="text" name="fim" value="fim2">value="t2 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t2 d2">
      <input type="text" name="inicio" value="t2 ini2">
      <input type="text" name="fim" value="t2 fim2">
   </div>
</form>

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   if(i == 0){
      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [{}];
   }else{
      
      obj[form1][0][$(this).attr("id")] = [{
         
        [$("[name='dia']", $(this)).val()] : [{
         
             "inicio" : $("[name='inicio']", $(this)).val(),
            "fim" : $("[name='fim']", $(this)).val(),
      
         }]
      }]
   }
   
});

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
<input type="text" name="nome" value="Fulano">
</form>
<form id="turno1">
 <input type="text" name="dia" value="dia1">
 <input type="text" name="inicio" value="ini1">
 <input type="text" name="fim" value="fim1">
</form>
<form id="turno2">
 <input type="text" name="dia" value="dia2">
 <input type="text" name="inicio" value="ini2">
 <input type="text" name="fim" value="fim2">
</form>

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   var $this = $(this);
   
   if(i == 0){ 

      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [{}]; 

   }else{
      
      obj[form1][0][$this.attr("id")] = [{}];

      $(".linha", $this).each(function(){
      
         obj[form1][0][$this.attr("id")][0][$("[name='dia']", $(this)).val()] = [{
       
               "inicio" : $("[name='inicio']", $(this)).val(),
               "fim" : $("[name='fim']", $(this)).val(),
         
         }]
      });
   }
   
});

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
   <input type="text" name="nome" value="Fulano">
</form> 

<form id="turno1">
   <div class="linha">
      <input type="text" name="dia" value="t1 d1">
      <input type="text" name="inicio" value="t1 ini1">
      <input type="text" name="fim" value="t1 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t1 d2">
      <input type="text" name="inicio" value="t1 ini2">
      <input type="text" name="fim" value="t1 fim2">
   </div>
</form> 

<form id="turno2">
   <div class="linha">
      <input type="text" name="dia" value="t2 d1">
      <input type="text" name="inicio" value="t2 ini1">
      <input type="text" name="fim" value="t2 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t2 d2">
      <input type="text" name="inicio" value="t2 ini2">
      <input type="text" name="fim" value="t2 fim2">
   </div>
</form>

removeu 52 caracteres do conteúdo
Fonte Link
Sam
  • 80,8mil
  • 22
  • 73
  • 127

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   if(i == 0){
      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [];[{}];
   }else{
      
      obj[form1][$obj[form1][0][$(this).attr("id")] = [{
         
        [$("[name='dia']", $(this)).val()] : [{
         
            "inicio" : $("[name='inicio']", $(this)).val(),
            "fim" : $("[name='fim']", $(this)).val(),
      
         }]
      }]
   }
   
});

console.log(obj[form1]['turno1']);
console.log(obj[form1]['turno2']obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
<input type="text" name="nome" value="Fulano">
</form>
<form id="turno1">
 <input type="text" name="dia" value="dia1">
 <input type="text" name="inicio" value="ini1">
 <input type="text" name="fim" value="fim1">
</form>
<form id="turno2">
 <input type="text" name="dia" value="dia2">
 <input type="text" name="inicio" value="ini2">
 <input type="text" name="fim" value="fim2">
</form>

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   if(i == 0){
      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [];
   }else{
      
      obj[form1][$(this).attr("id")] = [{
         
        [$("[name='dia']", $(this)).val()] : [{
         
            "inicio" : $("[name='inicio']", $(this)).val(),
            "fim" : $("[name='fim']", $(this)).val(),
      
         }]
      }]
   }
   
});

console.log(obj[form1]['turno1']);
console.log(obj[form1]['turno2']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
<input type="text" name="nome" value="Fulano">
</form>
<form id="turno1">
 <input type="text" name="dia" value="dia1">
 <input type="text" name="inicio" value="ini1">
 <input type="text" name="fim" value="fim1">
</form>
<form id="turno2">
 <input type="text" name="dia" value="dia2">
 <input type="text" name="inicio" value="ini2">
 <input type="text" name="fim" value="fim2">
</form>

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   if(i == 0){
      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [{}];
   }else{
      
      obj[form1][0][$(this).attr("id")] = [{
         
        [$("[name='dia']", $(this)).val()] : [{
         
            "inicio" : $("[name='inicio']", $(this)).val(),
            "fim" : $("[name='fim']", $(this)).val(),
      
         }]
      }]
   }
   
});

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
<input type="text" name="nome" value="Fulano">
</form>
<form id="turno1">
 <input type="text" name="dia" value="dia1">
 <input type="text" name="inicio" value="ini1">
 <input type="text" name="fim" value="fim1">
</form>
<form id="turno2">
 <input type="text" name="dia" value="dia2">
 <input type="text" name="inicio" value="ini2">
 <input type="text" name="fim" value="fim2">
</form>

Fonte Link
Sam
  • 80,8mil
  • 22
  • 73
  • 127

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   if(i == 0){
      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [];
   }else{
      
      obj[form1][$(this).attr("id")] = [{
         
        [$("[name='dia']", $(this)).val()] : [{
         
            "inicio" : $("[name='inicio']", $(this)).val(),
            "fim" : $("[name='fim']", $(this)).val(),
      
         }]
      }]
   }
   
});

console.log(obj[form1]['turno1']);
console.log(obj[form1]['turno2']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
<input type="text" name="nome" value="Fulano">
</form>
<form id="turno1">
 <input type="text" name="dia" value="dia1">
 <input type="text" name="inicio" value="ini1">
 <input type="text" name="fim" value="fim1">
</form>
<form id="turno2">
 <input type="text" name="dia" value="dia2">
 <input type="text" name="inicio" value="ini2">
 <input type="text" name="fim" value="fim2">
</form>