【问题标题】:Dynamically Creating HTML with Javascript not working [closed]使用 Javascript 动态创建 HTML 不起作用 [关闭]
【发布时间】:2012-10-12 22:45:32
【问题描述】:

没有 JQuery。我正在尝试创建一个单选按钮,后跟带有 javascript 的文本。它在 document.writes 之后打印出所有内容。我认为我的引号有问题,但我无法解决。有人可以帮我解决这个问题吗?

function printarray (newarray, cnt) { 
  if (cnt < 2) { 
        document.getElementById('radio').style.display = 'none'; 
  } else { 
    for (var i = 0; i < newarray.length; i++) { 
      var cur_text = newarray[i]; 
      if (i == 0) { 
        var radio = '<input type="radio" name="bank" value="KeyBank" onclick="showChoice(this)">' + 'KeyBank'; 
      } else { 
        radio = radio + '<input type="radio" name="bank" value="<script>document.write(cur_text);</script>" onclick="showChoice(this)">' + "<script>document.write(cur_text)</script>;"; 
      } 
    } 
    document.getElementById('btn').style.display = 'none'; 
    var foo = document.getElementById('radio').innerHTML = radio; 
  } 
  document.getElementById('array').innerHTML = cnt; 
} 

【问题讨论】:

  • 这样使用document.write()的目的是什么?
  • 这样使用 document.write() 非常危险,而且可能是错误的

标签: javascript html dynamic radio


【解决方案1】:

第一个问题是,下面一行是错误的,

radio = radio + '<input type="radio" name="bank" value="<script>document.write(cur_text);</script>" onclick="showChoice(this)">' + "<script>document.write(cur_text)</script>;";

修正线,

radio = radio + '<input type="radio" name="bank" value="'+cur_text+'" onclick="showChoice(this)">' + cur_text;

【讨论】:

  • 别忘了给我的答案投票。如果这回答有帮助,则将其设置为正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多