使用jQuery检查单选按钮不会触发收音机的onclick事件 [英] Checking radio button with jQuery doesn't trigger radio's onclick event

查看:75
本文介绍了使用jQuery检查单选按钮不会触发收音机的onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对收音机,我使用bind()在pages.ready事件上为其分配了一个函数。
然后,在同一个ready事件中,我检查另一个输入的值,如果它的值是1,则在页面加载时预先选择第二个单选按钮。
这里是一个片段。

 < input type =radioname =fpagoid =fpago1 value =1/> one 
< input type =radioname =fpagoid =fpago2value =2/> two

...

  $(document ).ready(function(){
$(#myspan)。hide();

$(#fpago1,#fpago2)。bind('change click',函数(){
togglePlazo();
});

//预选第二个广播节目的初始条件
grupo = $(#id_grupo)。 val();
if(grupo!='0'){
$(#fpago2)。attr('checked',true); //检查收音机,但不触发函数
}

});

...



- > 请参阅此处获取更完整的代码



问题在于,无线电如果条件满足,会得到检查,但是绑定函数 togglePlazo()不会触发...



如果稍后我手动单击单选按钮,则该功能会被触发,并且跨度会切换。它只是在jQuery上进行的初始检查,尽管广播发生了变化,函数并不会触发。



我不知道我失踪的是什么,也许我应该绑定更多的事件,而不是更改或点击...我只是无法找到我做错了什么。



注意:我正在使用jQuery 1.4。 2,但小提琴设置为使用1.6.4(它不具有1.4.2作为选项)

当您设置选中属性时,只需触发点击事件即可。

  $ (#fpago2)。attr('checked',true).trigger('click'); 

更改元素上的属性不会引发更改的事件...原生setAttribute或者使用jQuery的attr。

I have a pair of radios, to which I assign a function using bind(), on the pages .ready event. then, on the same ready event, I check for another input's value, and if it's value is "1" I preselect the second radio button when the page loads. here is a snippet.

<input type="radio" name="fpago" id="fpago1" value="1" />one
<input type="radio" name="fpago" id="fpago2" value="2" />two

...

$(document).ready(function() {
    $("#myspan").hide();

    $("#fpago1, #fpago2").bind('change click',function(){
        togglePlazo();
    });

//initial condition to preselect radio #2
    grupo = $("#id_grupo").val();
    if(grupo != '0'){
        $("#fpago2").attr('checked', true); //checks the radio, but doesn't trigger function
    }

});

...

--> see here for a more complete code

The problem is that, the radio does get checked if the condition is met, BUT the bound function togglePlazo() doesn't trigger...

If later I manually click the radio buttons, the function does get triggered, and the span toggles. It is only on the initial "check" made with jQuery, where the function does not trigger despite the radio getting changed.

I don't know what I'm missing, maybe I should bind more events other than change or click... I just can't find what I am doing wrong.

NOTE: I am using jQuery 1.4.2, but the fiddle is set to use 1.6.4 (it doesn't have 1.4.2 as an option)

解决方案

Just trigger the click event when you set the checked attribute.

$("#fpago2").attr('checked', true).trigger('click');

Changing an attribute on the element doesn't fire a changed event... Neither by the native setAttribute or using jQuery's attr.

这篇关于使用jQuery检查单选按钮不会触发收音机的onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆