如果我在选择列表中选择8,如何在表单上显示8个相同的内容?

时间:2017-07-22 03:41:42

标签: javascript html

以下是代码,如果您玩Quake III Arena,则更容易理解。

<select id="playerlist">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
</select>

我想打印&#34;播放器&#34;如果我选择8,表格上有8次; 7如果7; 6如果6 ...

示例:

player player player player player player player player
如果选择8

,将输出

1 个答案:

答案 0 :(得分:0)

使用此代码。获取select和use for循环的值以显示文本

document.getElementById("playerlist").addEventListener("change", function() {

  var selected = this.value;
  document.getElementById("text").innerHTML = "";
  var html = '';
  for (var i = 0; i < selected; i++) {
    html += '<div class="sub-text"> Player </div>';
  }
  document.getElementById("text").innerHTML = html;
});
#text{
  
}
.sub-text{
   display:inline-block;
   padding:2px;
}
<select id="playerlist" onchange="">
  <option value="0">Select Players</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
</select>

<div id="text">

</div>