论坛首页 Web前端技术论坛

利用JavaScript DOM函数动态创建radio buttons后在IE里无法做选择问题的解决

浏览 7838 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-10-10  
在一个项目里需要用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/
   发表时间:2007-09-17  
!已经解决,谢谢
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics