jquery to create array from form data

Posted by AndrewStevens on Stack Overflow See other posts from Stack Overflow or by AndrewStevens
Published on 2012-12-05T04:56:30Z Indexed on 2012/12/05 5:03 UTC
Read the original article Hit count: 126

I've been wrestling with this problem for a couple hours now.

Essentially, what I need to do is take the following or similar HTML:

<div id="excpdivs">
<div class="excpdiv" id="excpdiv0">
      Date: <input name="excp[0][date]">
      Open:  <input name="excp[0][open]">
      Close: <input name="excp[0][close]">
</div>
<div class="excpdiv" id="expdiv1">
      Date: <input name="excp[1][date]">
      Open:  <input name="excp[1][open]">
      Close: <input name="excp[1][close]">
</div>

and get an array similar to the following to a php script:

   Array
(
    [0] => Array
        (
            [date] => 2012-09-15
            [open] => 3:00
            [close] => 5:00
        )
   [1] => Array
        (
            [date] => 2012-09-16
            [open] => 2:00
            [close] => 5:00
        )

)

My main problem is getting the values from the input elements. My latest attempt is the following:

    var results = [];
$(".excpdiv").each(function(){
    var item = {};
    var inpts = $(this).find("input");
    item.date = $(inpts.get(0)).val();
    item.open = $(inpts.get(1)).val();
    item.close = $(inpts.get(2)).val();
    results.push(item);
});

Am I on the right track or am I hopelessly lost?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery