Reputation: 8959
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());
});
Upvotes: 2
Views: 213
Reputation: 40582
@Adil has it correct, but this can be simplified since you already have a reference to each element in this
when you call each:
$('.profile.gender').each(function() {
alert($(this).val());
});
Upvotes: 1
Reputation: 148150
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.
$('.profile').each(function(i) {
var self = this;
alert($('.gender').eq(i).val());
});
Upvotes: 3