Reputation: 25
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?
Upvotes: 1
Views: 1550
Reputation: 11318
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);
Upvotes: 1