Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS为输入的无线电类型组设置相同的属性名称_Javascript_Dom_Radio Button - Fatal编程技术网

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

Javascript 使用JS为输入的无线电类型组设置相同的属性名称,javascript,dom,radio-button,Javascript,Dom,Radio Button,我正在尝试用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

我正在尝试用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>

HTML5的标准不适用于自动计费

Vrai
人造的

L’LéLément div是一个非安全的部门

Vrai
人造的
但是,在控制台中,我的代码目前看起来更像这样:

<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>

HTML5的标准不适用于汽车行业。

L’LéLément div是一个非安全的部门。

显然,我当前的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";
问题=问卷长度;
newForm=document.createElement(“表单”);
newForm.id=“quizz”;
newTitle=document.createElement(“h1”);
对于(i=0;i<问题;i++){
new段落=document.createElement(“p”);
newparagration.classList=“问题”;
newInput=document.createElement(“输入”);
newInput.type=“收音机”;
newInput.id=“sp”;
newInput.value=“true”;
newInput.name=“vrai”;
newInput1=document.createElement(“输入”);
newInput1.type=“收音机”;
newInput1.id=“sp”;
newInput1.value=“false”;
newInput1.name=“faux”;
我曾尝试在循环中创建一个循环,并使用不同的slice和setattribute方法,但没有一个方法对我有效,甚至没有一个方法对我有意义


很抱歉提出了noob问题,感谢大家的帮助。

这有助于为每个问题提供两个以上的可能答案:

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);

});

您可以通过
表单[问题]
提交结果,其中
表单
是元素,
问题
“q0”
“q1”
在for循环中等。这有助于为每个问题提供两个以上的可能答案:

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);

});

您可以通过
表单[问题]
提交结果,其中
表单
是元素,
问题
“q0”
“q1”
在for循环中等。

不相关的点多次使用同一id不是好的做法。不相关的点多次使用同一id不是好的做法。