简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM