简体   繁体   English

使用JavaScript添加具有输入字段的表行

[英]Using JavaScript to add a table rows that have input fields

So I'm trying to add extra input fields within a table row using JavaScript, and I can't seem to figure out. 所以我试图使用JavaScript在表格行中添加额外的输入字段,我似乎无法弄清楚。 My approach right now is to just insert extra html code and add a new table row every time someone clicks the "add more" button. 我现在的方法是只需插入额外的HTML代码,并在每次有人点击“添加更多”按钮时添加新的表格行。 Here's my code right now 这是我现在的代码

HTML HTML

<div class="set-form">
<table class="table table-bordered">
<tr>
  <th>Question</th>
  <th>Answer</th>
</tr>
<tr>
  <td>
    <textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style="resize: none; width: 100%;"></textarea>
  </td>
  <td>
    <textarea name="Answer" placeholder="Answer" th:field="${questionAnswerSet.answer}" id="answer" style="resize: none; width: 100%;"></textarea>
  </td>
</tr>
</table>
<div class="set-form">
<input type="button" id="more_fields" onclick="add_fields();" value="Add More" class="btn btn-info" />
</div>

And Here's my JavaScript 这是我的JavaScript

function add_fields() {
document.getElementById('set-form').innerHTML += '<tr> < td > <   textarea name = "Question"
placeholder = "Question"
th: field = "${questionAnswerSet.question}"
id = "question"
style = "resize: none; width: 100%;" > < /textarea></td >
< td > < textarea name = "Answer"
placeholder = "Answer"
th: field = "${questionAnswerSet.answer}"
id = "answer"
style = "resize: none; width: 100%;" > < /textarea></td >
< /tr>'; }

It may be a little unorganized on here, but here's a link to a JSFiddle of the code http://jsfiddle.net/2t9Dh/357/ . 它可能在这里有点无组织,但这里是一个链接到代码http://jsfiddle.net/2t9Dh/357/的JSFiddle。 It's probably a pretty simple mistake, if anyone can see my issue and help me out, that would be great. 这可能是一个非常简单的错误,如果有人能看到我的问题并帮助我,那就太棒了。 Thanks in advance. 提前致谢。

UPDATE UPDATE

I'm closing the function, and updating the id and class names 我正在关闭该函数,并更新id和类名

Few issues are there with your HTML and JavaScript. 您的HTML和JavaScript几乎没有问题。

[1]You do not have any element with id "set-form". [1]您没有任何ID为“set-form”的元素。 Actually you have a div with class "set-form". 实际上你有一个div“class-form”。

I added id to the table and the updated HTML is below. 我在表中添加了id,更新的HTML如下所示。

<div class="set-form">
  <table id="tab_qn_ans" class="table table-bordered">
    <tr>
      <th>Question</th>
      <th>Answer</th>
    </tr>
    <tr>
      <td>
        <textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style="resize: none; width: 100%;"></textarea>
      </td>
      <td>
        <textarea name="Answer" placeholder="Answer" th:field="${questionAnswerSet.answer}" id="answer" style="resize: none; width: 100%;"></textarea>
      </td>
    </tr>
  </table>
  <div class="set-form">
    <input type="button" id="more_fields" onclick="add_fields();" value="Add More" class="btn btn-info" />
  </div>

[2]There are many spaces and carriage returns in your java script code. [2] java脚本代码中有许多空格和回车符。

Updated script code is 更新的脚本代码是

function add_fields() {
  document.getElementById('tab_qn_ans').innerHTML += '<tr> <td> <textarea name = "Question" placeholder = "Question" th: field = "${questionAnswerSet.question}" id = "question" style = "resize: none; width: 100%;" ></textarea></td> <td> <textarea name = "Answer" placeholder = "Answer" th: field = "${questionAnswerSet.answer}" id = "answer" style = "resize: none; width: 100%;"></textarea></td> </tr>';
}

I updated the same fiddle. 我更新了同样的小提琴。 It should work now. 它现在应该工作。

give a table Id write below js code. 给一个表Id写下js代码。

<div class="set-form">
  <table id="myTable" class="table table-bordered">
    <tr>
      <th>Question</th>
      <th>Answer</th>
    </tr>
    <tr>
      <td>
        <textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style="resize: none; width: 100%;"></textarea>
      </td>
      <td>
        <textarea name="Answer" placeholder="Answer" th:field="${questionAnswerSet.answer}" id="answer" style="resize: none; width: 100%;"></textarea>
      </td>
    </tr>
  </table>
  <div class="set-form">
    <input type="button" id="more_fields" onclick="add_fields();" value="Add More" class="btn btn-info" />
</div>


function add_fields() {    
document.getElementById("myTable").insertRow(0).innerHTML = '<tr><td><textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style = "resize: none; width:100%;"></textarea></td><td><textarea name="Answer" placeholder ="Answer" th: field="${questionAnswerSet.answer}" id="answer" style="resize:none;width: 100%;"></textarea></td ></tr>';
}

Append to last 追加到最后

function add_fields() {    
document.getElementById("myTable").insertRow(-1).innerHTML = '<tr><td><textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style = "resize: none; width:100%;"></textarea></td><td><textarea name="Answer" placeholder ="Answer" th: field="${questionAnswerSet.answer}" id="answer" style="resize:none;width: 100%;"></textarea></td ></tr>';
}

Demo 演示

Demo 2 演示2

There are a few problems with the function you were trying to write. 您尝试编写的功能存在一些问题。 You need to close the function. 你需要关闭这个功能。 Also, you are trying to select an element by ID, but no element has that ID. 此外,您尝试按ID选择元素,但没有元素具有该ID。 You would need to use getElementsByClassName() to find the element you were originally trying to find. 您需要使用getElementsByClassName()来查找最初尝试查找的元素。 The <div> element your were originally trying to target also was not closed. 您最初尝试定位的<div>元素也未关闭。

If you append another row to that element, it will not be within your table. 如果向该元素追加另一行,则它不在您的表中。 So, we need to select your table and append the html to your table. 因此,我们需要选择您的表并将html附加到您的表中。 We also will need to fix the structure of the table. 我们还需要修复表的结构。 It is currently missing <thead> & <tbody> elements. 它目前缺少<thead><tbody>元素。 I assume we can add those elements, but I will also assume that we can not add ID or class names. 我假设我们可以添加这些元素,但我还假设我们不能添加ID或类名。 Once these elements are added, we can add rows specifically to the <tbody> element. 添加这些元素后,我们可以专门向<tbody>元素添加行。

You also need to remove all extra spaces and (likely invisible) new line characters from the string of html you want to append. 您还需要从要追加的html字符串中删除所有多余的空格和(可能是不可见的)新行字符。

The finished product would look like this: 成品看起来像这样:

 function add_fields() { document.getElementsByClassName('table')[0].getElementsByTagName('tbody')[0].innerHTML += '<tr><td><textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style="resize: none; width: 100%;"></textarea></td><td><textarea name="Answer" placeholder="Answer" th:field="${questionAnswerSet.answer}" id="answer" style="resize: none; width: 100%;"></textarea></td></tr>'; } 
 <div class="set-form"> <table class="table table-bordered"> <thead> <tr> <th>Question</th> <th>Answer</th> </tr> </thead> <tbody> <tr> <td> <textarea name="Question" placeholder="Question" th:field="${questionAnswerSet.question}" id="question" style="resize: none; width: 100%;"></textarea> </td> <td> <textarea name="Answer" placeholder="Answer" th:field="${questionAnswerSet.answer}" id="answer" style="resize: none; width: 100%;"></textarea> </td> </tr> </tbody> </table> </div> <div class="set-form"> <input type="button" id="more_fields" onclick="add_fields();" value="Add More" class="btn btn-info" /> </div> 

问题是没有div宽度的id“set-form”,你将它作为一个类在两个地方。

The mix of Javascript and HTML code has already been addressed by the other answers. 其他答案已经解决了Javascript和HTML代码的混合问题。

I just want to address the IDs problem. 我只是想解决IDs问题。 If you want/need to have id property for each textarea element they need to be unique. 如果您希望/需要为每个textarea元素设置id属性,则它们必须是唯一的。 Here is a solution that increments the id , name and placeholder http://jsfiddle.net/068jtqLz/ 这是一个增加idnameplaceholder的解决方案http://jsfiddle.net/068jtqLz/

function add_fields() {
  var table = document.getElementById('myTable');

  var nextIndex = table.childNodes.length;

  var newRow = document.createElement("TR");
  var firstCell = document.createElement("TD");
  var firstCellTextarea = document.createElement("TEXTAREA");
  var secondCell = document.createElement("TD");
  var secondCellTextarea = document.createElement("TEXTAREA");

  firstCellTextarea.setAttribute("id", "question" + nextIndex);
  firstCellTextarea.setAttribute("name", "Question + nextIndex");
  firstCellTextarea.setAttribute("placeholder", "Question" + nextIndex);
  firstCellTextarea.setAttribute("th:field", "${questionAnswerSet.question}");
  firstCellTextarea.setAttribute("style", "resize: none; width: 100%;");

  secondCellTextarea.setAttribute("id", "answer" + nextIndex);
  secondCellTextarea.setAttribute("name", "Answer + nextIndex");
  secondCellTextarea.setAttribute("placeholder", "Answer" + nextIndex);
  secondCellTextarea.setAttribute("th:field", "${questionAnswerSet.answer}");
  secondCellTextarea.setAttribute("style", "resize: none; width: 100%;");

  firstCell.appendChild(firstCellTextarea);
  secondCell.appendChild(secondCellTextarea);

  newRow.appendChild(firstCell);
  newRow.appendChild(secondCell);

  table.appendChild(newRow);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM