我能';t动态访问文本区域';使用document.getElementById和普通JavaScript创建内容

我能';t动态访问文本区域';使用document.getElementById和普通JavaScript创建内容,javascript,html,json,Javascript,Html,Json,所以我有这个JSON格式 "questionList": [ { "questionText": "Create a function that adds 2 parameters", "questionId": "1", "testCase": "3,5" }, { "questionText": "Write a function that takes two integers, and subtract

所以我有这个JSON格式

"questionList": [
    {
        "questionText": "Create a function that adds 2 parameters",
        "questionId": "1",
        "testCase": "3,5"
    },
    {
        "questionText": "Write a function that takes two integers, and subtracts the second from the first.",
        "questionId": "12",
        "testCase": "10,3"
    },...
]
我正在动态创建文本区域,在这里我可以回答每个问题。这是我的代码

var questionsDiv = document.getElementById("questions");
for (let i = 0; i < questions.length; i++) {
  var div = document.createElement('div');
  div.className = 'question';
  div.innerHTML = '<h3>Question ' + parseInt(i+1) + '</h3><h5>' + questions[i].questionText + '</h5><textarea rows="10" cols="130" id="questionText"' + questions[i].questionId + ' placeholder="Enter your answer here..."></textarea><br><br>'

  questionsDiv.appendChild(div);
}

如何使用纯JS动态获取textareas的每个值?

我认为您放错了双引号:
id=“questionText””+问题[i]。questionId+”

应该是
id=“questionText”+问题[i]。questionId+”


或者您的所有问题都将具有相同的Id。

有几个问题,其中一个是双引号,在添加nr之前关闭您的Id值

<textarea rows="10" cols="130" id="questionText' + questions[i].questionId + '" placeholder="Enter your answer here..."></textarea>
//                                             ^ removed " here; added here   ^
然而,你不能做的是

console.log(document.question.questionText+questions[2].questionID.value);

这不会给您带来任何东西,因为这将是一个字符串连接。本质上,它可能会抛出一个错误。如果您需要使用calculation从对象获取属性,您可以始终使用
obj['questionText'+12]
包装您的计算,但是我认为您心目中的属性并不存在

我会跳过ID,使用类似于
document.querySelectorAll(“.question textarea”)
的东西来获取所有答案文本区域。他还不能用自己的声誉(需要15个);)啊,是的,错过了,谢谢你的支持,离我梦寐以求的500强如此之近,在那里我可以开始做评论来帮助社区:)
document.getElementById('questionText12')
console.log(document.question.questionText+questions[2].questionID.value);