所以我想使用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