使用键盘选择单选按钮时,更改事件未触发 [英] Change event not firing when radio button is selected with keyboard

查看:131
本文介绍了使用键盘选择单选按钮时,更改事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script>
$("input[name='my_radio_button']").change(function(){
    if ($("input[@name='my_radio_button']:checked").val() == 'ONE'){
                do_this_stuff(); 
    } else { do_other_stuff(); }
});
</script>

<input type="radio" name="my_radio_button1" id="radio1" value="ONE" checked />
<input type="radio" name="my_radio_button2" id="radio2" value="TWO" />

(假设完整的HTML和脚本在所有准备就绪时触发)

(assume complete HTML and the script firing when all is ready)

单击以选择单选选项时,更改事件似乎会触发,但使用键盘更改选择时不会触发.对此可以做任何事情吗?

The change event seems to fire when clicking to select a radio option, but not when the selection is changed with keyboard. Can anything be done about this?

edit-如果使用bindlive没什么不同-这只是一个错误吗?

edit - makes no difference if I use bind or live -- is this just a bug?

为澄清起见,即使失去焦点,事件也不会触发.

To clarify, the event does not fire even after focus is lost.

编辑2-没人知道原因吗?

edit 2 - nobody knows the reason for this?

编辑3-正如DonaldIsFreak指出的那样,这似乎是一个很简单的问题

edit 3 - as DonaldIsFreak pointed out this seems to be a chrome problem

推荐答案

这是可靠的修复方法 http://evilstreak.co.uk/blog/fixing-change-events-on-radios

使用它,这就是您通过示例实现它的方式: 这是演示 /1/">http://www.jsfiddle.net/uDkdJ/1/ 我在FF3.6,IE8,Safari5,Chrome7和Opera10.65中测试了此演示

And using it this is how you would implement it with your example: And here is a demo of the code below http://www.jsfiddle.net/uDkdJ/1/ I tested this demo in FF3.6, IE8, Safari5, Chrome7, and Opera10.65

$.fn.fix_radios = function() {
  function focus() {
    if ( !this.checked ) return;
    if ( !this.was_checked ) {
      $( this ).change();
    }
  }

  function change( e ) {
    if ( this.was_checked ) {
      e.stopImmediatePropagation();
      return;
    }
    $( "input[name=" + this.name + "]" ).each( function() {
      this.was_checked = this.checked;
    } );
  }
  return this.focus( focus ).change( change );
}

$(function() {
  $( "input[type=radio]" ).fix_radios();
  $("input[name='my_radio_button']").change(function(){
    if ($("input[@name='my_radio_button']:checked").val() == 'ONE'){
      do_this_stuff(); 
    } else { do_other_stuff(); }
  });
});

这篇关于使用键盘选择单选按钮时,更改事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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