简体   繁体   English

单击单选按钮时未收到更改事件

[英]Not getting change events when radio buttons are clicked

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. 因此,据我所知,单击未选中的名为pays的输入时,它将触发。

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! 谢谢!

I'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: 因此,当我单击名为pays的单选按钮时,如果使用以下命令,它将触发:

$('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. 我想这是从您的View.Try引号引起来的无线电处理程序。

'click input[name=pays]': radioHandler

And one advice: I had trouble with tutorials that uses older Backbone varsions ( < 0.9 ). 还有一个建议:我在使用较旧的Backbone varsions(<0.9)的教程中遇到麻烦。 So check your Backbone version. 因此,请检查您的骨干网版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM