javascript - 在javascript中动态地将用户输入字段中的数据添加到表中

标签 javascript html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
  <form >

    <label for="fname">First Name</label>
    <input type="text" id="fname" name="firstname" placeholder="Your name..">

    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastname" placeholder="Your last name..">

    <label for="country">Country</label>
    <select id="country" name="country">
      <option value="australia">Australia</option>
      <option value="canada">Canada</option>
      <option value="usa">USA</option>
    </select>

    <label for="subject">Subject</label>
    <textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>

    <input type="submit" value="Submit"  onclick="doFunction();">
  </form>
  </div>
  <table style="width:100%"  id = "myTable">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>country</th>
  </tr>
    <style>

    input[type=text], select, textarea {
        width: 100%; /* Full width */
        padding: 12px; /* Some padding */
        border: 1px solid #ccc; /* Gray border */
        border-radius: 4px; /* Rounded borders */
        box-sizing: border-box; /* Make sure that padding and width stays in place */
        margin-top: 6px; /* Add a top margin */
        margin-bottom: 16px; /* Bottom margin */
        resize: vertical /* Allow the user to vertically resize the textarea (not horizontally) */
    }

    /* Style the submit button with a specific background color etc */
    input[type=submit] {
        background-color: #4CAF50;
        color: white;
        padding: 12px 20px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
    }

    /* When moving the mouse over the submit button, add a darker green color */
    input[type=submit]:hover {
        background-color: #45a049;
    }

    /* Add a background color and some padding around the form */
    .container {
        border-radius: 5px;
        background-color: #f2f2f2;
        padding: 20px;
    }
</style>
<script>
function doFunction(){
  var value = document.getElementById("fname").select();
  var lastname = document.getElementById("lname").select();
  var  country = document.getElementById("Country").select();
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 =  row.insertCell(1);
  var cell3 =  row.insertCell(2);
  cell1.innerHTML = "value";
  cell2.innerHTML = "lastname";
  cell3. innerHTML = "country";
}
  </script>



</body>

当用户将数据输入文本框并单击提交按钮时,我们有空文本框,数据应添加到我们在您的 html 页面中创建的表格中
我在 java 脚本中使用 select by id tags 和 insert row 方法在 javascript 中动态地将元素添加到表中 我试过了,但没有得到想要的输出 提前致谢

最佳答案

您的代码存在这些问题。

  • 您忘记关闭 table 标签
  • 您需要使用 .value 而不是 .select() 来获取输入字段的值
  • 您需要将获取的存储在变量中的值传递给单元格的innerHTML,而不是硬编码字符串
  • 如果您不想重新加载页面,您需要将event.preventDefault() 添加到提交事件监听器
  • getElementById('Country') 应该是 getElementById('country')

我已将 theadtbody 添加到您的 html 中,以便您可以直接插入表体而不是表,并将该表更改为 var table = document.querySelector("#myTable tbody");

function doFunction(){
  var value = document.getElementById("fname").value;
  var lastname = document.getElementById("lname").value;
  var country = document.getElementById("country").value;
  var table = document.querySelector("#myTable tbody");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 =  row.insertCell(1);
  var cell3 =  row.insertCell(2);
  cell1.innerHTML = value;
  cell2.innerHTML = lastname;
  cell3.innerHTML = country;
}

const submit = document.querySelector('#submitBtn');
submit.addEventListener('click', event => {
  event.preventDefault();
  doFunction();
});
input[type=text], select, textarea {
  width: 100%; /* Full width */
  padding: 12px; /* Some padding */
  border: 1px solid #ccc; /* Gray border */
  border-radius: 4px; /* Rounded borders */
  box-sizing: border-box; /* Make sure that padding and width stays in place */
  margin-top: 6px; /* Add a top margin */
  margin-bottom: 16px; /* Bottom margin */
  resize: vertical /* Allow the user to vertically resize the textarea (not horizontally) */
}

/* Style the submit button with a specific background color etc */
input[type=submit] {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/* When moving the mouse over the submit button, add a darker green color */
input[type=submit]:hover {
  background-color: #45a049;
}

/* Add a background color and some padding around the form */
.container {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}
  <div class="container">
  <form >

    <label for="fname">First Name</label>
    <input type="text" id="fname" name="firstname" placeholder="Your name..">

    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastname" placeholder="Your last name..">

    <label for="country">Country</label>
    <select id="country" name="country">
      <option value="australia">Australia</option>
      <option value="canada">Canada</option>
      <option value="usa">USA</option>
    </select>

    <label for="subject">Subject</label>
    <textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>

    <input type="submit" value="Submit" id="submitBtn">
  </form>
  </div>
  <table style="width:100%"  id = "myTable">
  <thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>country</th>
  </tr>
  </thead>
  <tbody>
  </tbody>
</table>

关于javascript - 在javascript中动态地将用户输入字段中的数据添加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50581561/

相关文章:

javascript - 如何修复 ' ctx.clearRect is not a function'

javascript - 在 Angular 中访问嵌套的json并在php中显示

html - 在导航栏上滚动的 Flexbox 元素

javascript - 如何让我的按钮不可点击

jquery - 使用 jQuery 删除表格行

javascript - jQuery 从 Excel 复制到多个输入字段

javascript - 如何在 angular.js 中动态应用自定义过滤器?

css - 如何将 div/行与流体背景颜色混合

javascript - 隐藏没有 li 子元素的父元素

javascript - 在 React 状态下使用嵌套对象