JavaScript循环中的HTML

HTML inside of a JavaScript loop

本文关键字:HTML 循环 JavaScript      更新时间:2023-09-26

我正在尝试创建10个单选按钮,标记为1-10,在我的html中有一个for循环,但我无法让它工作。

<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <script>
                for (var i = 1; i <=10; i++) {
                    <input type="radio" name="scores" id="i" value="i"> i
                }
            </script>
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
</html>

谢谢。我是JS的新手,所以我仍然在学习很多东西。

使用document.write像这样输出HTML。

document.write('<input type="radio" name="scores" id="i" value="i"> i');

<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <script>
                for (var i = 1; i <=10; i++) {
                    document.write('<input type="radio" name="scores" id="i" value="i">'+ i);
                }
            </script>
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
</html>

您可以将所有输入添加到循环内的一个字符串中,然后将其附加到HTML中,还应该将jshtml代码分开

var inputs = '';
 for (var i = 1; i <= 10; i++) {
   inputs += '<input name="scores" type="radio" value="' + i + '" id="' + i + '">'+i+'';
 }
 document.getElementById('NPSform').insertAdjacentHTML('afterbegin', inputs);
<h2 style="text-align:center">On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?</h2>
<form id="NPSform" style="text-align:center">
  <input type="submit" name="mysubmit" value="Submit" />
</form>

您还可以创建一个div,设置innerHTML并使用insertBefore将其添加到html 中

var inputs = '';
for (var i = 1; i <= 10; i++) {
  inputs += '<input name="scores" type="radio" value="' + i + '" id="' + i + '">' + i + '';
}
var div = document.createElement('div');
div.innerHTML = inputs;
var submit = document.querySelector('#NPSform input');
submit.parentNode.insertBefore(div, submit);
<h2 style="text-align:center">On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?</h2>
<form id="NPSform" style="text-align:center">
  <input type="submit" name="mysubmit" value="Submit" />
</form>

<html>
<head>
<script>
  function addbuttons() {
    var buttons = document.getElementById("dynamicButtons");
            for (var i = 1; i <=10; i++) {
                buttons.innerHTML += '<input type="radio" name="scores" id="' +i +' " value="' +i +'">'
            }
            }
 </script>
</head>
<body onload="addbuttons()">
  <h2 style="text-align:center">
    On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
  </h2>
    <form id="NPSform"; style= "text-align:center">     
        <div id="dynamicButtons"></div>
        <input type="submit" name="mysubmit" value="Submit"/>
    </form>
</body>
</html>
<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
    <script>
        for (var i = 1; i <=10; i++) {
            document.write("<input type='radio' name='scores' id='"+i+"' value='"+i+"'> "+i+"");
        }
    </script>
</html>
<html>
    <body>
      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
      <form id="NPSform"; style= "text-align:center">
        <div id="radioscontainer">
        </div>
        <input type="submit" name="mysubmit" value="Submit"/>
      </form>

<script>
  var radioButtons = '';
  for (var i = 1; i <=10; i++) {
    radioButtons += '<input type="radio" name="scores" id="i" value="i"> i';
  }//for
  $("#radioscontainer").append( radioButtons );
</script>
</body>
</html>