-2
私は、ラジオボタンを動的に作成するいくつかのプロブラムを持っています。私の問題では、私はそれがそうでなければラジオボタンを作成する必要があるオプションが含まれているかどうかを確認するよりもjson formateでサーバーからデータを要求しています単純に答えを提出するフィールドのテキスト領域を作成します。再び私はjson formateでそれを解析し、サーバーに送信し、私の質問が次の質問などのための新しいURLを取得している場合は... 私のラジオボタンを作成し、そこからデータを読み取ることができます{「答え」:「何か」}のように、そのデータを解析するよりも。私のコードは怒鳴るです:JSでラジオボタンを作成する方法。
enter code herefunction loadDoc(url) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
var data = JSON.parse(this.responseText);
document.getElementById("my_test").innerHTML = data.question;
// Send the answer to next URL
if(data.alternatives !== null){
createRadioElement(div,);
}
var answerJSON = JSON.stringify({"answer":"2"});
sendAnswer(data.nextURL, answerJSON)
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function sendAnswer(url, data) {
xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var data = JSON.parse(this.responseText);
console.log(this.responseText);
loadDoc(data.nextURL);
}
}
// var data = JSON.stringify({"email":"[email protected]","password":"101010"});
xhr.send(data);
}
function createRadioElement(name, checked) {
var radioHtml = '<input type = "radio" name="' + name + '"';
if (checked) {
radioHtml += ' checked="checked"';
}
radioHtml += '/>';
var radioFragment = document.createElement('div');
radioFragment.innerHTML = radioHtml;
return radioFragment.firstChild;
}
あなたがた。 –
...いいえ。 ['document.createElement'](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement)は何も挿入しません。 –
.innerHTMLはありません –