IE7 radio button issue when created dynamically

喜夏-厌秋 提交于 2019-12-11 05:54:10

问题


I have an issue in IE7 when I am creating clones of radio buttons. I am dynamically updating the name and ID attributes, however, I still have the issue that a radio button being checked resets any of the others which have been created dynamically. Any idea how this can fixed? Here is a fiddle of the issue

This is the JS code which manipulates the form fields:

// Dropdown select
$('#quantity').live("change", function(){

    $('.questions_clonable:not(.questions_clonable:first)').remove();


    // Get value of selection
    var num = $(this).val();

    var cloned_el = $('.questions_clonable').clone();   

    if (num > 1)
    {  
        for (var i = 1; i < num; i++)
        {
            // Assign cloned block to new var
            var new_block = cloned_el;  

             // Store previous number for replacing with current in cloned block input fields
                var prev = i-1;

                // Update input name to make it unique
                new_block.find('input').each(function() {     
                    this.name = this.name.replace(prev, i); 
                    this.id = this.id + i; 
                });

            // Bit of a workaround needed to clone properly, reiterating class name
            $('.multiple_questions_container').append('<span class="questions_clonable hidden">'+new_block.html()+'</span>');

        }   
    }
});​

回答1:


IE7 has problems with dynamically created radio buttons, which is weird because it's such an amazing browser. It seems IE7 won't let you rename the name attribute of radio buttons.

Here is a possible workaround.




回答2:


I found several possible fixes for this but the following worked perfectly:

function setElementName(elems, name) {
        if ($.browser.msie === true){
            $(elems).each(function() {
                this.mergeAttributes(document.createElement("<input name='" + name + "'/>"), false);
            });
        } else {
            $(elems).attr('name', name);
        }
    } 


来源:https://stackoverflow.com/questions/13585939/ie7-radio-button-issue-when-created-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!