Developer
Developer

Reputation: 1040

Jquery: save field values into a multidimensional array

Js Fiddle

jsfiddle

What I Require

In the above fiddle I have got 2 text fields, a checkbox and a text. What I require is I need to get the checkbox value and 2 text field values into an array of this structure.

 array= array(
              [0]=>array(
                         [0]=>checkbox1,
                         [1]=>field1,
                         [2]=>input1),
              [1]=>array(
                         [0]=>checkbox2,
                         [1]=>field2,
                         [2]=>input2)

              )

Edit

Im building this application in codeigniter. I need to post this array using ajax and process it in php. So I want the values in either array format or json as shown above.

Upvotes: 1

Views: 801

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$('#add').click(function() {
    var result = $('#mytable tr:has(input:checkbox:checked)').map(function() {
        var $this=$(this), row =[];
        row.push($this.find('input[name="checker1"]').is(':checked'));
        row.push($this.find('input[name="field1"]').val());
        row.push($this.find('input[name="input1"]').val());
        return [row]
    }).get();
    console.log(result);
});

Demo: Fiddle

Upvotes: 5

Related Questions