1

How to get values of group of textbox and put them in a (key : value) array using JQuery?

foreach ($students as $value) {
    ?>
    <tr>
        <td>
            <input name="result[]" class="result" id="<?= $value['stuId'];?>" type="text" required=""/>
        </td>
    </tr>
    <?php
        }
    ?>

I don't know how to get values of a group of input values.

Which technique is useful in that case and how can I make the keys of the array is the input id?

Jawa
  • 2,336
  • 6
  • 34
  • 39
A Hagrasi
  • 25
  • 5
  • Do you want array, or object? http://stackoverflow.com/questions/1144705/best-way-to-store-a-key-value-array-in-javascript – sinisake Apr 06 '15 at 21:46
  • Well .. My Intention was to send that array through $.post("URL",{array : myArray},function(data){//code}); and manipulate that array in the URL located function. What is better an object or an array ? – A Hagrasi Apr 06 '15 at 21:55
  • Both will work...you can use json format, if you wish... – sinisake Apr 06 '15 at 22:18
  • oh ya I'd like to learn that .. can you recommend me a link to learn this technique ? – A Hagrasi Apr 06 '15 at 22:33

1 Answers1

1

Demo: http://jsfiddle.net/uyko2ahx/

Depending on your needs, you can use simple array, or object, with key:value pair:

arr=[];
$( ".result" ).each(function( index ) {
 key=$( this ).prop('id');
 value=$(this).val();
    arr.push(key+':'+value);   
});

console.log(arr);

obj={};

$( ".result" ).each(function( index ) {
 key=$( this ).prop('id');
 value=$(this).val();

    obj[key]=value; 
});

console.log(obj);
sinisake
  • 11,240
  • 2
  • 19
  • 27