【发布时间】:2019-08-07 12:56:37
【问题描述】:
对于上下文,我已链接 Bootstrap CDN,但由于某种原因我无法在此处复制/粘贴。
当我运行此代码时,第一次提交会创建一个带有id='masterTable' 的表格行,但每次我点击提交之后,程序只会写入同一个表格行,而不是创建一个新行。
我想在每次点击提交时创建一个新的表格行,但我在这里被难住了。有什么想法吗?
<!DOCTYPE html>
<html>
<link>
</link>
<head>
</head>
<body>
<div class="d-flex">
<div>
<select id="ddlViewBy">
<option value="0310">XXXX-10</option>
<option value="4610" selected="selected">XXXX-10</option>
<option value="4610">XXXX-10</option>
</select>
</div>
</div>
<form>
<div class="form-group">
<label>Hand Trucks</label>
<input type="text" class="form-control" id="handtrucks" placeholder="Enter
number of Hand Trucks.">
</div>
<div>
<label>Furniture Pads</label>
<input type="text" class="form-control" id="furniture" placeholder="Enter
number of Furniture Pads.">
</div>
<button type="submit" onClick='addLocation(); return false;' id='button'
class="btn btn-primary">Submit</button>
</form>
<tbody id='masterTable'>
</tbody>
</table>
</body>
<script>
const button = document.getElementById('button');
const dropdown = document.getElementById('dropdown').value;
const htruck = document.getElementById('handtrucks').value;
const fpad = document.getElementById('furniture').value;
function addLocation() {
let masterList = document.getElementById('masterTable');
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
var handTruck = document.getElementById('handtrucks').value;
var furnPads = document.getElementById('furniture').value;
var row = document.createElement("tr");
var locationEntry = document.createElement('td');
var htEntry = document.createElement('td');
var fpEntry = document.createElement('td');
row.appendChild(locationEntry);
locationEntry.appendChild(document.createTextNode(strUser));
row.appendChild(htEntry);
htEntry.appendChild(document.createTextNode(handTruck));
row.appendChild(fpEntry);
fpEntry.appendChild(document.createTextNode(furnPads));
masterList.appendChild(locationEntry);
masterList.appendChild(htEntry);
masterList.appendChild(fpEntry);
}
</script>
</html>
【问题讨论】:
标签: javascript forms input html-table bootstrap-4