简体   繁体   English

我试图找出为什么当我运行以下代码时,单选按钮的值显示在控制台上而不显示在浏览器上的原因?

[英]I am trying to find out why my value for the radio button is shown on the console but not on the browser when I run the following code?

    for (var i = 0; i < allQuestions[n].choices.length; i++) {
        var input = document.createElement("input");
        input.type = "radio";
        input.name = "choices";
        input.value = "choice" + i;
        var answer = document.getElementById("answer");
        var option = document.createTextNode(allQuestions[n].choices[i]);
        input.appendChild(option);
        answer.appendChild(input);
    }

http://jsfiddle.net/slopeofhope81/g9V9d/ http://jsfiddle.net/slopeofhope81/g9V9d/

input elements cannot have children : input元素不能有孩子

The input element is a void element. 输入元素是一个void元素。 An input element must have a start tag but must not have an end tag. 输入元素必须具有开始标签,但不能具有结束标签。

and

A void element is an element whose content model never allows it to have contents under any circumstances. void元素是其内容模型在任何情况下都不允许其内容的元素。 Void elements can have attributes. 空隙元素可以具有属性。

You can add the text after the input: 您可以在输入之后添加文本:

var option = document.createTextNode(allQuestions[n].choices[i]);
answer.appendChild(input);
answer.appendChild(option);

DEMO DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试运行我的代码时会收到 TypeError? - Why am I getting a TypeError when trying to run my code? 为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用? - why my code doesn't work when I am trying to concatenate a function's return value with a string? 我试图在控制台上显示“嗨,我是大卫,数字是偶数”,但每次我运行代码时,“偶数”都会显示为数字 - I'm trying to get "Hi I am David , and the number is even" shown on the console, but every time I ran the code "even" is shown as a number 我正在尝试了解以下javascript代码 - I am trying to understand the following javascript code 当我试图确保我的 javascript 代码正常工作时,我在 Google Chrome 上的控制台上收到此错误 - I get this error on my console on Google Chrome when I am trying to make sure that my javascript code is working 我试图让一个框在单击按钮时淡出? - I am trying to get a box to fade out when a button is clicked? 我正在尝试记录提交按钮表单的元素,但在控制台浏览器中看不到任何内容 - I am trying to log the elements of my submit buttons form but it I cannot see anything in the console browser 为什么我没有得到包含以下代码的表格? - Why am I not getting a table with the following code? 为什么当我运行以下代码时多维数据集没有出现? - Why is the cube not appearing when I run the following code? 尝试运行我的机器人命令时出现此错误 - I am getting this error when trying to run my bot commands
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM