Is there a way to get the custom HTML data-*
attributes for the selected radio button when you submit a form? The value does not seem to get picked up by serializeArray()
.
HTML
<form id="preference-form">
<table>
<tr class ="result">
<td width="100%">{{Title}}</td>
<td><input type="radio" id="radio-{{Project_No}}-1" data-application="{{Application_ID}}" name="{{Project_ID}}" value="1"></td>
<td><input type="radio" id="radio-{{Project_No}}-2" data-application="{{Application_ID}}" name="{{Project_ID}}" value="2"></td>
<td><input type="radio" id="radio-{{Project_No}}-3" data-application="{{Application_ID}}" name="{{Project_ID}}" value="3"></td>
<td><input type="radio" id="radio-{{Project_No}}-9" data-application="{{Application_ID}}" name="{{Project_ID}}" value="9"></td>
</tr>
</table>
</form>
JavaScript
$("#preference-form).on('submit', function() {
var data = $(this).serializeArray();
console.log(data)
});
This outputs the name
and value
fields, but I can't seem to find a simple answer about the data-*
fields. Unfortunately, I need all three pieces of information in order to perform an update on the database record, and from what I understand:
I think the tricky part for this is the multiple elements compared to a single element.
Based on comment that all you have is radios; writing your own serializer is simple.
First I would put the data attribute on the <tr>
for less repetition
<tr data-application="{{Application_ID}}">
Then you would do something like:
var data = $(this).find('tr:has(:radio:checked)').map(function(){
var $row=$(this), radio = $row.find(':radio:checked')[0]
return {
app: $row.data('application'),
name: radio.name,
value: radio.value
}
}).get()