I’m trying to make a quiz for myself and I want the data values of the selected radio buttons and the data value of them. So I can send them to a php file as an array or object.
This is my code:
let frm = $('#form');
$('input[value="finished"]').on('click', function (e) {
e.preventDefault();
console.log($('input[type=radio]:checked').data('value'));
$.ajax({
type: frm.attr('method'),
url: "ajax/quiz.php",
data: frm.serialize(),
success: function (data) {
},
}).done(function (data) {
});
});
Does anyone know why it only shows one? Because I have been searching for a while and a lot of people recommend this option.
Can't get input radio selected value
I also tried this:
How to get the selected radio button’s value?
But it still shows me one data value.
2
Answers
You can do this in many ways. I show below two wrong ways and two correct ways:
If you insist on saving the values inside
dataset
(<input data-value="" />
) you can do it this way:Now,
values
is an array of alldata-value
values in selected radio boxes.You can read more about
jQuery
‘smap
here: https://api.jquery.com/map/