搜索墙首页  编程技术  java  asp  ajax  php  c/c#/c++  数据库  oracle  mysql  db2  操作系统  windows  linux  股吧
首页 > 编程 > ajax > JavaScript
 1         
1楼  boogie 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/
2楼  woogy 2007-09-17

!已经解决,谢谢
 1         
您的发言将按有关规定都会存档,您须为所发表后果负责,请您遵纪守法并注意语言文明。
标题:利用JavaScriptDOM函数动态创建radiobuttons后在IE里无法做选择问题的解决
热门关注
标题回复点击
[源码]XiorkFlow:用JavaScript写的工作流设计器73540
推荐一个不错的免费JavaScript开发工具,比aptana轻得多~~70425
jquery和prototype框架的比较。。。62370
大胆预测下JS框架的走势106357
开心网【朋友买卖、咬人、争车位】Firefox插件118307
Mootools前景?拿来主义VS重复发明轮子102301
Mootools和Jquery之间的选择问题60270
Mootools、jQuery之说三道四64246
js操作html的table,包括添加行,添加列,删除行,删除列,合并单元格(未实现)29214
关于Gecko的eval第二参数探密51209
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源