2

I have form which contains more (dynamic) wrap-segments, I am trying to create object that can be passed through jquery ajax and stored in DB.

PROBLEM is how to create multidimensional object?

this is code I have

DB:

  formID              wrapSegmentID           
  pkt                 formID  FK
  hldy                st
                      tt
                      rt

HTML:

<form name="uniqID>                     
  <div name="ws" class="wrapSegment">
    <input name="st">
    <input name="tt">
    <input name="rt">
  </div>
  <div name="ws" class="wrapSegment">
    <input name="st">
    <input name="tt">
    <input name="rt">
  </div>

  <!--it can be more .wrapSegment divs--> 
  <!--it can be more .wrapSegment divs-->
  <!--it can be more .wrapSegment divs-->

<label>
  <input class="hldy" type="checkbox"> hldy
</label>
<label>
  <input class="pkt" type="checkbox"> pkt
</label>
<a class="save">Save</a>
</form> 

JS

$('.hero-unit').on('click','.save', function (){
   var day = {  
         dayID : "id",  
         pkt: "pkt",
         hldy: "hldy",
         ws:  $(".wrapSegment").each(function() {                           
                var inputs = $(this).children(":input");
                var wsObj = $.map(inputs, function(item, y) {
                        return {
                            key: item.name,
                            value: $(item).val()
                        };
                 });            
               return (wsObj);
        })
    };  



 console.log(day); //
 return false;          
});

I am getting "ws: parent divs"

console log

but I wish to have it like this

Object { dayID="id", pkt=0, hldy=1, ws=[Object { Object { key="1",value="1"}, Object { key=2, value=2}, {Object { key="1",value="1"}, Object { key=2, value=2} ] }

maybe is the way I am trying to submit form wrong?

0

1 Answer 1

1

$.each will return a jQuery object. What you want to do is something like:

$('.hero-unit').on('click','.save', function (){
   var wsObj = [];
   $(".wrapSegment").each(function() {                           
         var inputs = $(this).children(":input");
         var inputArray = $.map(inputs, function(item, y) {
                 return {
                            key: item.name,
                            value: $(item).val()
                        };
                 });      
         wsObj.push(inputArray);      
   });
   var day = {  
         dayID : "id",  
         pkt: "pkt",
         hldy: "hldy",
         ws:  wsObj
    };  

 console.log(day); //
 return false;          
});
Sign up to request clarification or add additional context in comments.

1 Comment

you have added wsObj.push(inputArray); i have to test it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.