[英]im trying to add a user prompt to add into the table i have but i cant figure it out
I'm trying to make it to where when the user is asked the question it will input into the cell on the table. 我试图将其输入到询问用户问题的位置,它将输入到表格的单元格中。
I'm needing to the ask a user to enter the answer to the question asked and I've looked all over and can't seem to find a way. 我需要让用户输入所问问题的答案,我已经四处张望,似乎找不到办法。
var trip; var miles; var gallons; var mpg; trip = parseFloat(prompt("Enter your trip location")); miles = parseFloat(prompt("Enter miles driven")); gallons = parseFloat(prompt("Enter gallons of gas used"));
<table id="myTable"> <tr> <td> |Trip Name|</td> <td> |Miles Driven| </td> <td> |Gallons Used| </td> <td> |MPG| </td> </tr> <tr> <td>Row 2 cell1 </td> <td>Row2 cell2</td> <td>Row2 cell3</td> <td>Row2 cell4</td> </tr> <tr> <td>Row3 cell1</td> <td>Row3 cell2</td> <td>Row3 cell3</td> <td>Row3 cell4</td> </tr> <tr> <td>Row4 cell1</td> <td>Row4 cell2</td> <td>Row4 cell3</td> <td>Row4 cell4</td> </tr> <tr> <td>Row5 cell1</td> <td>Row5 cell2</td> <td>Row5 cell3</td> <td>Row5 cell4</td> </tr> </table>
Saving the answer given to a prompt in variable won't make it appear in the document. 将提示提示的答案保存在变量中不会使其出现在文档中。 To do that, you must select the cell wherein you want it to appear and set its textContent
property as shown below: 为此,必须选择要显示它的单元格并设置其textContent
属性,如下所示:
var cell = document.querySelector("selector-of-the-cell");
cell.textContent = prompt("...");
In your current JavaScript
code, you're repeatedly doing the same thing using the same code over and over again. 在当前的JavaScript
代码中,您一次又一次地使用相同的代码重复执行相同的操作。 As your application scales up, things are going to get messy there. 随着应用程序的扩展,事情会变得一团糟。
An alternative I suggest is using a data-*
attribute, like data-question
for instance, and save each question to the column title it concerns. 我建议的替代方法是使用data-*
属性,例如data-question
,然后将每个问题保存到其关注的列标题中。 This way, in order to make the code work as desired you will only need to: 这样,为了使代码按预期工作,您只需要:
prompt
. 遍历标题,并在prompt
使用每个人的问题。 textContent
of the cell under the title. 将结果保存在标题下单元格的textContent
中。 You can see the above suggestion implemented in the following working snippet. 您可以在以下工作片段中看到上述建议。
Snippet: 片段:
/* ----- JavaScript ----- */ /* Get all column titles. */ var titles = document.querySelectorAll("#myTable th"); var cells = document.querySelectorAll("#myTable td"); /* Iterate over every title. */ [].forEach.call(titles, function(title, index) { /* Get the question associated with the title. */ var question = title.dataset.question; /* Parse and save the result of the question in the cell under the title. */ cells[index].textContent = prompt(question); });
/* ----- CSS ----- */ #myTable { border-collapse: collapse; } #myTable tr>* { padding: .5em; border: 1px solid #ccc; }
<!---- HTML -----> <table id="myTable"> <thead> <th data-question="Enter your trip location">Trip Name</th> <th data-question="Enter miles driven">Miles Driven</th> <th data-question="Enter gallons of gas used">Gallons Used</th> <th data-question="Enter your speed">MPG</th> </thead> <tbody> <tr> <td>Row1 cell1</td> <td>Row1 cell2</td> <td>Row1 cell3</td> <td>Row1 cell4</td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.