问题
I'm using plain vanilla JavaScript to clone fieldsets and change the ID/name attributes of any of the form fields within the fieldset.
Everything works properly, except in IE7 the cloned radio buttons are all treated as one group. For example, in one fieldset, I have two radio buttons. When I clone it and add another fieldset, then click in the last radio button of the new fieldset, it unchecks the very first radio button.
This appears to be a bug in IE7 where it doesn't change the name attribute (even though everything is working properly in other browsers). I'm using input.getAttribute("name") and have also tried input.htmlName, but neither makes a difference.
Does anyone have any tips on how to get IE7 to treat radio button group for each fieldset as a separate group?
Here's the code I'm using:
input.setAttribute("name", inputName + fieldsetNumber);
Regular text INPUTs work fine, the issue just involves IE7 and radio buttons. From everything I've dug up, there's a bug in IE7 with radio buttons.
回答1:
Ended up using the solution posted here: http://code.rawlinson.us/2006/03/another-ie-gotcha-dynamiclly-created.html to get around this issue.
回答2:
This works, tested.
function changeName(targetId,newName){
document.getElementById(targetId).name = newName;
}
来源:https://stackoverflow.com/questions/9336173/how-to-get-ie7-to-change-name-attribute-of-radio-buttons-using-plain-javascript