javascript - 在html中自动将元素添加到表中

标签 javascript html

我在表单中有三个输入列表和一个提交按钮。 表格下面也有一张 table 。 我希望当我单击提交按钮时,无需重新加载页面即可在表格中查看我的输入值。

<input name="Choose1">
<input name="Choose2">
<input name="Choose3">
<input type="submit" value"Add">

<table>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
</tr>
<tr>
<td>Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
</tr>
</table>

很明显第一个表应该是空的。 add 我应该多次添加一个元素

最佳答案

使用jquery

$("#addRow").on('click', function(){
  const html = "<tr><td>"+$('#td1').val()+"</td><td>"+$('#td2').val()+"</td><td>"+$('#td3').val()+"</td></tr>";
  $('#bodyTable').append(html)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="Choose1" id="td1">
<input name="Choose2" id="td2">
<input name="Choose3" id="td3">
<button id="addRow" >Add </button>

<table>
  <thead>
  <tr>
    <th>Field 1</th>
    <th>Field 2</th>
    <th>Field 3</th>
  </tr>
  </thead>
  <tbody id="bodyTable">
  <tr>
    <td>Value 1</td>
    <td>Value 2</td>
    <td>Value 3</td>
  </tr>
  </tbody>
</table>

关于javascript - 在html中自动将元素添加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57766722/

相关文章:

javascript - 用于 Scriptaculous/Prototype DOM 元素的 Colspan

javascript - 如何更改我搜索的文本颜色?

HTML 单选按钮(如条形图)和位于其下方的相应标签(如 X 轴名称)

javascript - 合并两个 Canvas

javascript - 如何在Jquery中缩放div内容?

javascript - Magento 单页结账步骤未扩展

javascript - 获取特定类的每个 UL 元素的 ID

javascript - 使用 Javascript 将 HTML 文件转换为 Excel 文件

javascript - 如何在框架内加载 jQuery?

javascript - Angular 指令和 Controller 响应 ng-click 事件