JQuery 和 RadioButtons 问题
我有一张包含单选按钮(即 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IE(所有版本)在
复选框
和单选按钮
上的onChange
事件实现确实存在错误。如果您想提供可靠的跨浏览器行为,则必须使用
onClick
事件。请参阅此链接了解更多信息。
IE (all versions) have a really buggy implementation of the
onChange
event oncheckboxes
andradio buttons
.You have to use the
onClick
event if you want to provide a solid cross-browser behaviour.See this link for more info.
显然这可以通过使用
而不是 .change 来解决。至少在与 ie 的兼容性方面。
有人知道为什么改变失败或者很可能我做错了什么吗?
apparently this can be solved with using
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?