使用JS为输入的无线电类型组设置相同的属性名称

问题描述 投票:1回答:1

我正在尝试使用JS动态构建类似的东西:

<p class = "questions">La norme HTML5 ne nécessite pas de guillemets autour des valeurs d'attribut.</p>
<input type = "radio" id = "sp" name = "question1" value = "true"> Vrai<br>
<input type = "radio" id = "sp" name = "question1" value = "false"> Faux<br>

<p class = "questions">L'élément &lt;div&gt; est un élément non sémantique.</p>
<input type = "radio" id = "sp" name = "question2" value = "true"> Vrai<br>
<input type = "radio" id = "sp" name = "question2" value = "false"> Faux<br>

但是,在控制台中,我的代码目前更像这样:

<form id="quizz">
<p class="questions">La norme HTML5 ne nécessite pas de guillemets autour des valeurs d'attribut.
<input type="radio" id="sp" value="true" name="vrai">
<input type="radio" id="sp" value="false" name="faux">
</p>
<p class="questions">L'élément &lt;div&gt; est un élément non sémantique.
<input type="radio" id="sp" value="true" name="vrai">
<input type="radio" id="sp" value="false" name="faux"></p>

很显然,我当前的JS代码很丑陋:

questions = questionnaire.length;
newForm = document.createElement("form");
newForm.id = "quizz";
newTitle = document.createElement("h1");
for (i = 0; i < questions; i++) {

newParagraph = document.createElement("p");
newParagraph.classList = "questions";

newInput = document.createElement("input");
newInput.type = "radio";
newInput.id = "sp";
newInput.value = "true";
newInput.name = "vrai";

newInput1 = document.createElement("input");
newInput1.type = "radio";
newInput1.id = "sp";
newInput1.value = "false";
newInput1.name = "faux";

我曾尝试在循环中创建一个循环,并使用不同的slice和setattribute方法,但似乎没有一个对我有意义,甚至没有意义。

对不起,这个问题很感谢,谢谢大家的帮助。

javascript dom radio-button
1个回答
0
投票
这有助于为每个问题提供两个以上的答案:

function makeForm() { const newForm = document.createElement( "form" ); document.querySelector(".container").appendChild(newForm); // Instead of .map() you can use a for loop through the questions questions.map((question, qIndex) => { // Each question with the radios is a p element const questionElement = document.createElement("p"); // Set the question text questionElement.innerText = question; // Loop through the different possible answers. // Two items for the two possible answers, // in each of two, [0] is value, [1] is the text in french [['true', 'Vrai'], ['false', 'Faux']].map(answer => { const radioElement = document.createElement( "input" ); radioElement.setAttribute("type", "radio"); // id has 'sp', question index and true or false radioElement.setAttribute("id", `sp-${qIndex}-${answer[0]}`); // name is 'q' and question index, it has to be the same for each question radioElement.setAttribute("name", `q${qIndex}`); // value is true or false, the string you want when form is submitted radioElement.setAttribute( "value", `${ answer[ 0 ]}`); // Create label connected to each answer const label = document.createElement('label'); // Needs a for attribute to connect id to the id of this radio label.setAttribute( "for", `sp-${ qIndex }-${ answer[ 0 ]}`); // Label text is 'vrai' or 'faux' label.innerText = answer[1]; questionElement.appendChild(radioElement); questionElement.appendChild(label); }); newForm.appendChild(questionElement); });

您可以通过form[question]提交结果,其中form是元素,question"q0",在for循环中是"q1",等等
© www.soinside.com 2019 - 2024. All rights reserved.