JQuery 和 RadioButtons 问题

发布于 2024-08-16 10:07:56 字数 994 浏览 4 评论 0原文

我有一张包含单选按钮(即 3x3 网格)的表格,我希望当我选择其中一个时,包含单选按钮的颜色会发生变化。按照这个 示例 我这样做了

<table class="table-name">
<tr>
    <td>
        <span>Some text</span>
        <input type="radio" name="some-name" />
    </td>
    <td>
        <span>Some text</span>
        <input type="radio" name="some-name" />
    </td></tr>
</table>

而且 javascript

$(':radio').change(function() {           
    $('.color-1').removeClass('color-1');       
    var $td = $(this).parent('td');
    if (this.checked) {               
        $td.addClass('color-1');   
    } else {                          
        $td.removeClass('color-1');
    }
});

在 Firefox 上运行良好。但在 Internet Explorer 上,它会为之前选择的内容着色 因此,如果我选择 1,1,它会保持白色,但当我选择 1,2 时,1,1 会变成蓝色,依此类推。

有什么想法吗?

I have a table containing radiobuttons (i.e. 3x3 grid) and I want when I select one of them the containing the radiobutton to change colour. Following this example I did this

<table class="table-name">
<tr>
    <td>
        <span>Some text</span>
        <input type="radio" name="some-name" />
    </td>
    <td>
        <span>Some text</span>
        <input type="radio" name="some-name" />
    </td></tr>
</table>

and the javascript

$(':radio').change(function() {           
    $('.color-1').removeClass('color-1');       
    var $td = $(this).parent('td');
    if (this.checked) {               
        $td.addClass('color-1');   
    } else {                          
        $td.removeClass('color-1');
    }
});

this works well on firefox. but on internet explorer it colors the previously selected
So if I select 1,1 it stays white but when I select 1,2 then 1,1 turns blue and so on.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-08-23 10:07:56

IE(所有版本)在复选框单选按钮上的onChange事件实现确实存在错误。
如果您想提供可靠的跨浏览器行为,则必须使用 onClick 事件。

请参阅此链接了解更多信息。

IE (all versions) have a really buggy implementation of the onChange event on checkboxes and radio buttons.
You have to use the onClick event if you want to provide a solid cross-browser behaviour.

See this link for more info.

新人笑 2024-08-23 10:07:56

显然这可以通过使用

$(':radio').click(function() ...

而不是 .change 来解决。至少在与 ie 的兼容性方面。

有人知道为什么改变失败或者很可能我做错了什么吗?

apparently this can be solved with using

$(':radio').click(function() ...

instead of the .change. at least in terms of compatibility with ie.

anyone has a clue why change fails or most probably what I do wrong?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文