开发者

jquery onchange event for radio button now instantaneous

I am trying to make an ID active only when a certain type of radio but开发者_开发问答ton is selected. Below is my jQuery code:

$(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 SomeKind it does not make Type active instantaneously, instead I have to click again (anywhere on the page) for Type to be active.

I am on IE7


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:

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

You can use Chinmayee's .click approach (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:

$(':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.


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');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜