Ввод данных непосредственно в таблицу - PullRequest
1 голос
/ 06 января 2020

Я делаю простой сайт по математике для подруги, и ей нужен какой-то аспект вероятности. Я пытался отобразить количество бросков кубиков на столе. Это то, что у меня есть.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Click the button to display the number you have rolled on the dice.</p>

<button onclick="myFunction()">Roll the Dice!</button>

<p id="demo2"></p>

<table> 
<tr>
 <th>Number</th>
 <th>Times Rolled</th>
 </tr>
 <tr>
 <td>One</td>
 <td>"value"</td>
 <tr/>
 <tr>
 <td>Two</td>
 <td>"value"</td>
 </tr>
 <tr>
 <td>Three</td>
 <td>"value"</td>
 </tr>
  <tr>
 <td>Four</td>
 <td>"value"</td>
 </tr>
  <tr>
 <td>Five</td>
 <td>"value"</td>
 </tr>
  <tr>
 <td>Six</td>
 <td>"value"</td>
 </tr>
 
 </table>
 


<script>
function myFunction() {
  var x = Math.floor((Math.random() * 6) + 1);
  document.getElementById("demo2").innerHTML = x;
}
</script>

это создает случайный бросок костей.

Затем мне нужно, чтобы каждый бросок костей попал в таблицу ниже в нужном месте.

Я хочу, чтобы каждый бросок кубиков добавлялся в таблицу, чтобы в любой момент пользователь мог остановиться и посмотреть, сколько раз он бросил какое-то число.

Может ли кто-нибудь мне помочь? веселит.

1 Ответ

2 голосов
/ 06 января 2020

Вы можете сделать что-то ниже

var i = 1;
function myFunction() {
  var x = Math.floor((Math.random() * 6) + 1);
  document.getElementById("demo2").innerHTML = x;
  
  var newRow=document.getElementById('numberTable').insertRow();
  newRow.innerHTML = "<td>"+i+"</td><td> "+x+"</td>";
  i++;
}
 <p>Click the button to display the number you have rolled on the dice.</p>

<button onclick="myFunction()">Roll the Dice!</button>

<p id="demo2"></p>




<table id="numberTable"> 
<tr>
 <th>Number</th>
 <th>Times Rolled</th>
 </tr>

 

 </table>
...