Mates, I'm tryin' to set backbone events when radio buttons are clicked. I have the following buttons:
<p><label for="pays">Pays now</label><input id="pays" name="pays" type="radio" value="1" class="_100"/></p>
<p><label for="pays2">Pays later</label><input id="pays2" name="pays" type="radio" value="2" class="_100"/>
And I define the event like this:
'click input[name=pays]:not(:checked)': 'radioChecked'
So, as I understand it, it should fire when clicking an input named pays that's not checked.
The thing is, that is not firing any event when I click on a radio button.
If I try this...
'click input[name=pays]': radioClicked
I can fix this with some flags, but i'll have the event fired every time any radio is clicked.
Any idea? Thanks!
Mates, I'm tryin' to set backbone events when radio buttons are clicked. I have the following buttons:
<p><label for="pays">Pays now</label><input id="pays" name="pays" type="radio" value="1" class="_100"/></p>
<p><label for="pays2">Pays later</label><input id="pays2" name="pays" type="radio" value="2" class="_100"/>
And I define the event like this:
'click input[name=pays]:not(:checked)': 'radioChecked'
So, as I understand it, it should fire when clicking an input named pays that's not checked.
The thing is, that is not firing any event when I click on a radio button.
If I try this...
'click input[name=pays]': radioClicked
I can fix this with some flags, but i'll have the event fired every time any radio is clicked.
Any idea? Thanks!
Share Improve this question edited May 18, 2013 at 22:02 Pablo asked May 18, 2013 at 21:42 PabloPablo 1,1414 gold badges18 silver badges46 bronze badges3 Answers
Reset to default 5I've found a solution. I changed the event declaration to:
'click input[name=pays]:checked': 'onRadioClick'
So now it triggers when I click on any radio button named pays, and if I use:
$('input[name=pays]:checked').val()
I get the checked radio value, instead of the first one declarated.
Thanks!
Maybe I'm not fully understanding but seems as though this should work fine.
$(document).ready(function () {
$('input[name="pays"]').click(function () {
if($(this).prop('checked')){
// do something here
console.log('i am checked');
}
});
});
// .prop available in jQuery 1.6 or above.
I suppose it is from your View.Try to put quotes around radio handler.
'click input[name=pays]': radioHandler
And one advice: I had trouble with tutorials that uses older Backbone varsions ( < 0.9 ). So check your Backbone version.