-2
我有一些problrm动态创建单选按钮。在我的问题中,我正在从json合成器中的服务器请求数据,而不是检查它是否包含选项,我必须创建单选按钮,否则只需创建字段的txt区域以提交答案。再次我解析它在JSON甲酸并发送到服务器,如果我的问题是写我得到新的网址为下一个问题等等... 我的问题是我如何创建单选按钮,并从它读取数据和比解析像{“答案”:“东西”}的数据。我的代码波纹管:如何通过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;
}
你们我正在通过radioFragment.innerHTML = radioHtml注入; –
...没有。 ['document.createElement'](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement)不会注入任何内容。 –
否但.innerHTML确实 –