单选按钮的 jquery onchange 事件现在是即时的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4311071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:04:00  来源:igfitidea点击:

jquery onchange event for radio button now instantaneous

jqueryhtml

提问by learn_plsql

I am trying to make an ID active only when a certain type of radio button is selected. Below is my jQuery code:

我试图仅在选择某种类型的单选按钮时激活 ID。下面是我的 jQuery 代码:

$(document).ready(function() {
   $('#Type').attr('disabled', 'disabled');
   $('input[@name=action]').change(function() {
     if ($("input[@name=action]:checked").val() == 'SomeKind')              
        $('#Type').removeAttr('disabled');
     else 
        $('#Type').attr('disabled', 'disabled');
    });
});

This works as intended but with one flaw. When I click on the radio button SomeKindit does not make Typeactive instantaneously, instead I have to click again (anywhere on the page) for Typeto be active.

这按预期工作,但有一个缺陷。当我单击单选按钮时,SomeKind它不会Type立即激活,而是必须再次单击(页面上的任何位置)Type才能激活。

I am on IE7

我在 IE7

采纳答案by Chinmayee G

if action is a name of your checkbox, then use click event instead of change,

如果操作是您的复选框的名称,则使用单击事件而不是更改,

$('input[@name=action]').click(function() {
     if (this.checked && $(this).val() == 'SomeKind')              
        $('#Type').removeAttr('disabled');
     else 
        $('#Type').attr('disabled', 'disabled');
});

回答by mu is too short

IE7 has some odd issues with change events and radio buttons, the change event doesn't fire until there's a blur as well. I ran into similar issues once and, after much gnashing off teeth, found this:

IE7 在更改事件和单选按钮方面有一些奇怪的问题,更改事件在出现模糊之前不会触发。我曾经遇到过类似的问题,在咬牙切齿之后,发现了这个:

http://norman.walsh.name/2009/03/24/jQueryIE

http://norman.walsh.name/2009/03/24/jQueryIE

You can use Chinmayee's .clickapproach (but check and double check that everything works properly when you use the keyboard rather than the mouse) or you can add a click handler to all your radio buttons that blurs and refocuses:

您可以使用 Chinmayee 的.click方法(但是当您使用键盘而不是鼠标时,请检查并仔细检查一切是否正常工作),或者您可以向所有模糊和重新聚焦的单选按钮添加单击处理程序:

$(':radio').click(function() {
    try {
        this.blur();
        this.focus();
    }
    catch (e) {}
});

You only want this disgusting kludge for IE7 so you'd probably want to wrap it in a conditional comment (a kludge within a kludge). I'm pretty sure IE8's radio buttons have the same problem.

您只需要 IE7 的这个令人厌恶的 kludge,因此您可能希望将它包装在条件注释中(kludge 中的 kludge)。我很确定 IE8 的单选按钮也有同样的问题。