所以我想使用JavaScript在表行中添加额外的输入字段,我似乎无法弄清楚。我现在的做法是插入额外的html代码,并在每次有人点击“添加更多”按钮时添加一个新的表格行。这里是我的代码,现在使用JavaScript添加一个具有输入字段的表行
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>
,这里是我的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>'; }
它可能是一个有点散乱放在这里,但这里的代码http://jsfiddle.net/2t9Dh/357/的的jsfiddle的链接。这可能是一个非常简单的错误,如果任何人都可以看到我的问题并帮助我,那会很棒。提前致谢。
UPDATE
我关闭该功能,并更新ID和类名
你检查错误控制台的解决方案? –
您在标记中使用'set-form'类时正在执行'document.getElementById('set-form')'。 –
是的,它说'未捕获的ReferenceError:add_fields没有定义',我对JavaScript相当新,所以我不完全确定这意味着什么 – dochsner