vendredi 2 janvier 2015

If checkbox is checked, get the contents of other inputs

I'm trying to get contents of 'grouped' inputs, depending on whether the checkbox of that 'group' is checked. I then want to take the contents of each input in the group and put it in an array. Below are two group examples generated by a PHP loop, each group input will have a group associated number at the end of the name. If need be, I can change the names, ID's, etc.



<input name="hold_1" id="hold_1" value="1" type="checkbox">
<input name="hold_id_1" id="hold_id_1" value="200" type="hidden">
<input name="hold_date_1" id="hold_date_1" value="2014" type="text">

<input name="hold_2" id="hold_2" value="2" type="checkbox">
<input name="hold_id_2" id="hold_id_2" value="250" type="hidden">
<input name="hold_date_2" id="hold_date_2" value="2014" type="text">


This is the JQuery statement that I could use some help with. This version successfully gets all the input values, but doesn't distinguish between which group is checked.



var inputs = $('input'), dataArray = {};
$.each(inputs, function (index, input) {
dataArray[$(input).attr('id')] = $(input).val();
});


So far, by using various methods, I've only been able to get all the input values (regardless of whether checked), or only checkbox values, or some other data that isn't useful to me.


In case it helps, here's the .ajax post that uses the that data array. This part is working fine.



$.ajax({
url: "something.php",
data: dataArray,
type: 'POST',
success: function (data) {
$(doStuff);
}
});


Any help is appreciated.





Aucun commentaire:

Enregistrer un commentaire