I’m having trouble with jQuery and KnockoutJS. I’m trying to read a form with radio buttons where you can choose if you’re male/female.
profile().gender value is ‘m’ so the ‘checked’ thing works. But in the jQuery I should get both alert for ‘m’ and ‘f’ but I get ‘m’ twice.
I have this HTML:
<input type="radio" class="profile gender" name="gender" data-bind="value: 'm', attr: { checked: profile().gender=='m' }" /> Male
<input type="radio" class="profile gender" name="gender" data-bind="value: 'f', attr: { checked: profile().gender=='f' }" /> Female
Then read it with this jQuery code:
$('.profile').each(function() {
var self = this;
alert($('.gender').val());
});
The statement you have to in alert
$('.gender').val()will always give you first element, you need to give index of element to go through them.