简体   繁体   English

我正在尝试添加用户提示以添加到我拥有的表中,但我无法弄清楚

[英]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> 

Short Answer 简短答案

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("...");

Long Answer 长答案

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: 这样,为了使代码按预期工作,您只需要:

  • get the titles (aka the cells of the first row) and the cells of the second row. 获取标题(又名第一行的单元格和第二行的单元格。
  • iterate over the titles and use each one's question inside prompt . 遍历标题,并在prompt使用每个人的问题。
  • save the result in the 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.

相关问题 我试图为我的训练营做一个测验,但我不知道代码有什么问题 - im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code 我的捕获错误,我似乎无法弄清楚 - Im getting an error with my catch that i cant seem to figure out 好的,我正在尝试弄清楚如何向缩略图中添加文本(下面是完整的新手代码) - Ok so im trying to figure out how to add text to thumbnails (im a complete novice code below) 无法弄清楚使用变量的语法我必须搜索用户是否有角色 - cant figure out the syntax to use the variables i have to search if a user has a role 我有这个 python 代码,我试图将其转换为 javascript 但我似乎无法弄清楚是哪个 if 语句。 使用 - I have this python code that I am trying to turn into javascript but I cant seem to figure out which if statement. to use 我试图弄清楚如何使用函数在对象中添加值 - I'm trying to figure out how to add the values in an object using a function 我不知道的滚动错误 - scrolling bug that i cant figure out 我尝试制作的Javascript模块系统出现问题,无法弄清 - Issue with Javascript module system I'm trying to make and cant figure out 我添加了一个本地存储 function 但它似乎不适用于我创建的调度程序,我不知道为什么 - I have added a local storage function but it doesnt seem to work on my Scheduler i have created and i cant figure out why 我有一个php文件,并且在里面的代码中有一个错误,我不知道该如何解决? 以及在哪里添加其余代码 - I have a php file and in the code inside i have one error i can't figure out how to fix it ? And where to add the rest of the codes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM