A-A+
Ajax radio button bug
在一个项目里需要用JavaScript动态建立radio buttons,然后替换span element下的内容,在IE里能显示出来,可就是无法做选择,郁闷了半天,后来查网上资料发现是IE的BUG,说是 "IE doesn’t allow the name attribute to be changed after the element is created“,可我试了下Checkbox就不存在此问题。
原代码:
- function createCxtjRadioButtons(data,cxtjId,cxtjTagId){
- var tagCxtj = document.getElementById(cxtjTagId);
- tagCxtj.removeChild(tagCxtj.childNodes[0]);
- for (var key in data) {
- var val = data[key];
- var input = document.createElement("input");
- input.setAttribute("name",cxtjId);
- input.setAttribute("type","radio");
- input.setAttribute("value",key);
- tagCxtj.appendChild(input);
- var label = document.createElement("label");
- label.setAttribute("for",input_id);
- label.appendChild(document.createTextNode(val));
- tagCxtj.appendChild(label);
- }
- }
解决后:
- function createCxtjRadioButtons(data,cxtjId,cxtjTagId){
- var tagCxtj = document.getElementById(cxtjTagId);
- tagCxtj.removeChild(tagCxtj.childNodes[0]);
- for (var key in data) {
- var val = data[key];
- var input = createElement("input", cxtjId);
- input.setAttribute("type","radio");
- input.setAttribute("value",key);
- tagCxtj.appendChild(input);
- var label = document.createElement("label");
- label.setAttribute("for",input_id);
- label.appendChild(document.createTextNode(val));
- tagCxtj.appendChild(label);
- }
- }
- function createElement(type, name) {
- var element = null;
- try {
- // First try the IE way; if this fails then use the standard way
- element = document.createElement('<'+type+' name="'+name+'">');
- } catch (e) {
- // Probably failed because we’re not running on IE
- }
- if (!element) {
- element = document.createElement(type);
- element.name = name;
- }
- return element;
- }
参考:
1、http://www.thunderguy.com/semicolon/2005/05/23/setting-the-name-attribute-in-internet-explorer/
2、http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/