简体   繁体   English

在IE6,IE7中单击时动态添加的单选按钮未更新

[英]Dynamically added radio button not updated when clicked in IE6, IE7

I have added a group of radio buttons dynamically to the DOM using jQuery. 我使用jQuery动态地向DOM添加了一组单选按钮。 I have registered event handlers, which work well. 我已经注册了事件处理程序,它运行良好。 However, when clicked in IE6 and IE7, the radio button group is not updated with which button is checked. 但是,在IE6和IE7中单击时,单选按钮组不会更新选中的按钮。

My original code is analogous to this: 我的原始代码类似于:

<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>

Again, when I click a radio button, the event handler is called correctly, but the radio buttons are not updated. 同样,当我单击一个单选按钮时,会正确调用事件处理程序,但不会更新单选按钮。 I have made this hack which seems to work: 我做了这个似乎有用的黑客:

<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>

Basically, what my fix is doing is to uncheck all the buttons in the radio button group, then mark the one clicked as checked. 基本上,我的修复工作是取消选中单选按钮组中的所有按钮,然后将单击的按钮标记为已选中。 It seems to be a bit hacky. 这似乎有点哈哈。 Is there a better way to handle this? 有没有更好的方法来处理这个? Has anyone come across this problem and can give some more insight into the why this behaves the way it does? 有没有人遇到过这个问题,可以更深入地了解它为什么会这样做?

Thanks. 谢谢。

This is a old IE problem with form elements. 这是表单元素的旧IE问题。 It's not the Jquery that are to blame here. 这不应该归咎于Jquery。 If you want a work-around for IE 6 user then you can make your click function as this 如果您想为IE 6用户进行解决,那么您可以将点击功能设置为此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM