Do not receive changes when clicking radio buttons

Helpers, I'm trying to set up basic events when I click radio buttons. 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 as follows:

 'click input[name=pays]:not(:checked)': 'radioChecked' 

So, as I understand it, it should work when you click on the input with the name "pays", which is not checked.

The fact is that I do not turn off any event when I press the switch.

If I try this ...

 'click input[name=pays]': radioClicked 

I can fix this with some flags, but I will fire an event every time any radio station is pressed.

Any idea? Thanks!

+4
source share
3 answers

I have found a solution. I changed the event declaration to:

 'click input[name=pays]:checked': 'onRadioClick' 

So, now it works when I click on any radio button with the name "pays", and if I use:

 $('input[name=pays]:checked').val() 

I get the value of the checked radio, not the first one announced.

Thanks!

+5
source

Perhaps I do not quite understand, but it seems that 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. 
+1
source

I suppose this is from your View.Try to put quotes around the radio handler.

 'click input[name=pays]': radioHandler 

And one piece of advice: I had problems with tutorials that use older Backbone varsions (<0.9). So check your base version.

0
source

Source: https://habr.com/ru/post/1481568/


All Articles