【问题标题】:Dynamically added radio button not updated when clicked in IE6, IE7在 IE6、IE7 中单击时动态添加的单选按钮未更新
【发布时间】:2010-12-13 17:28:33
【问题描述】:

我使用 jQuery 向 DOM 动态添加了一组单选按钮。我已经注册了事件处理程序,效果很好。但是,在 IE6 和 IE7 中单击时,单选按钮组不会更新为选中哪个按钮。

我原来的代码是这样的:

<body>
<form action="radio-ie7_submit" method="get" accept-charset="utf-8"></form>
<script type="text/javascript" charset="utf-8">
    $(function() {
        $.each(['A','B', 'C'], function (i, e) {
            $('<input>')
                .attr('type', 'radio')
                .attr('name', 'foo')
                .attr('checked', i == 1 ? 'checked' : '')
                .val(e)
                .appendTo($('form'))
                .after(e + '<br />')
                .click(function(){
                    alert('Kliiik')
                })
        })  
    })
</script>
</body>

再次,当我单击单选按钮时,事件处理程序被正确调用,但单选按钮没有更新。我做了这个似乎有效的破解:

<body>
<form action="radio-ie7_submit" method="get" accept-charset="utf-8"></form>
<script type="text/javascript" charset="utf-8">
    $(function() {
        $.each(['A','B', 'C'], function (i, e) {
            $('<input>')
                .attr('type', 'radio')
                .attr('name', 'foo')
                .attr('checked', i == 1 ? 'checked' : '')
                .val(e)
                .appendTo($('form'))
                .after(e + '<br />')
                .click(function(){
                    $('input[name="' + this.name + '"]').each(function () { this.checked = false });
                    this.checked = true;
                    alert('Kliiik')
                })
        })  
    })
</script>
</body>

基本上,我的修复方法是取消选中单选按钮组中的所有按钮,然后将单击的按钮标记为选中。这似乎有点hacky。有没有更好的方法来处理这个?有没有人遇到过这个问题,并且可以更深入地了解为什么会这样?

谢谢。

【问题讨论】:

  • 又是和jquery live相关的东西////???
  • 我不确定这是否是 jQuery 特有的问题。

标签: javascript jquery internet-explorer-7 internet-explorer-6 radio-button


【解决方案1】:

这是一个旧的 IE 表单元素问题。这不是 Jquery 的责任。如果您想为 IE 6 用户提供解决方法,那么您可以将点击功能设为这样

            .click(function(){
                $('input[name="' + this.name + '"]').attr('checked',''); 
                this.checked = true; 
                alert('Kliiik')                
            })

【讨论】:

  • 决定使用 .removeAttr('checked') 代替 .attr('checked', '') 和 .attr('checked', true) 代替 this.checked = true。跨度>
  • 感谢您确认这不是 jQuery 问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 2022-07-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多