2012-10-23 38 views
-4

沒有JQuery。我正在嘗試創建一個單選按鈕,然後是使用javascript的文本。它是在document.writes之後打印出來的。我想我的報價有問題,但我無法修復。有人可以幫我解決這個問題嗎?使用Javascript動態創建HTML不起作用

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; 
} 
+1

是什麼),使用'文件撰寫的目的('這樣呢? – nnnnnn

+0

這是非常危險的,可能是錯誤的使用document.write()這樣的 –

回答

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; 
+0

不要忘記投我的答案。如果這有助於解決問題,請將其設置爲正確答案。 –