我编写了一个JavaScript代码,用于将食物添加到列表中。对于行中的每个食物,都有一个删除按钮。但如果我按下按钮没有任何反应。我在代码中找不到错误。我在newRow.innerHtml + = ...中定义了id =“delete”的按钮。我还有一个方法deleteRow(element)但由于某种原因它不会删除。有人可以帮我找到问题吗?
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<title>Test</title>
<link rel="stylesheet" href="style.css" />
</head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 50%;}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 2px;}
tr:nth-child(even) {
background-color: #dddddd;}
</style>
<body>
<h3>Foods</h3>
<table id="table">
<tr>
<th>Name</th>
<th>Ingredients</th>
<th>Meat</th>
<th>Vegetarian</th>
<th>Vegan</th>
</tr>
</table>
</section>
<br><br>
<!-- FORMULAR -->
<section>
<h3>New food</h3>
<p>Name: <input type="text" id="name" />
<p>Ingredients: <input type="text" id="ingredients" />
<br><br>
<input type="radio" id="meat" name="select" value="meat" />
<label for="meat">Meat</label>
<input type="radio" id="veg" name="select" value="vegetarian" />
<label for="veg">vegetarian</label>
<input type="radio" id="vegan" name="select" value="vegan" />
<label for="vegan">vegan</label>
<p>Passwort: <input type="password" id="passwd" />
<button id="add" onclick="Add()">Add</button>
<script>
let table = document.getElementById("table");
document.addEventListener('DOMContentLoaded', registerCallbacks);
function registerCallbacks(){
function Add() {
var newRow = document.createElement('tr');
var name = document.getElementById("name").value;
var ingredients = document.getElementById("ingredients").value;
var pw = document.getElementById("passwd").value;
var rObj = document.getElementsByName("select");
var vegetarian = rObj[1].checked;
var meat = rObj[0].checked;
var vegan = rObj[2].checked;
newRow.innerHTML += "<td>" + name + "</td><td>" + ingredients +"</td><td>" + meat+ "</td><td>" + vegetarian +"</td><td>" + vegan + "</td><td>" + '<button id="delete" onclick="deleteRow(this)">Delete Row</button>'
document.getElementById("table").appendChild(newRow);
}
var addButton = document.getElementById('add');
addButton.addEventListener( 'click', function(evt){Add(); evt.preventDefault();});
var deleteButton = document.getElementById('delete');
deleteButton.addEventListener('click', function(evt){deleteRow(this); evt.preventDefault();});
function deleteRow(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
}
}
</script>
</section>
</article>
</main>
<!-- FOOTER -->
<footer>
<p>© Copyright 2017</p>
</footer>
</div>
</body>
</html>
答案 0 :(得分:2)
这应该有效
<body>
<!-- FORMULAR -->
<section>
<table id="table">
<tr>
<th>Name</th>
<th>Ingredients</th>
<th>Meat</th>
<th>Vegetarian</th>
<th>Vegan</th>
</tr>
</table>
<h3>New food</h3>
<p>Name: <input type="text" id="name" />
<p>Ingredients: <input type="text" id="ingredients" />
<br><br>
<input type="radio" id="meat" name="select" value="meat" />
<label for="meat">Meat</label>
<input type="radio" id="veg" name="select" value="vegetarian" />
<label for="veg">vegetarian</label>
<input type="radio" id="vegan" name="select" value="vegan" />
<label for="vegan">vegan</label>
<p>Passwort: <input type="password" id="passwd" />
<button id="add" onclick="return addRow()">Add</button>
<script>
var table = document.getElementById("table");
function addRow() {
var newRow = document.createElement('tr'),
name = document.getElementById("name").value,
ingredients = document.getElementById("ingredients").value,
pw = document.getElementById("passwd").value,
rObj = document.getElementsByName("select"),
vegetarian = rObj[1].checked,
meat = rObj[0].checked,
vegan = rObj[2].checked;
newRow.innerHTML += "<td>" + name + "</td><td>" + ingredients +"</td><td>" + meat+ "</td><td>" + vegetarian +"</td><td>" + vegan + "</td><td>" + '<button id="delete" onclick="return deleteRow(this)">Delete Row</button>'
document.getElementById("table").appendChild(newRow);
return false;
}
function deleteRow(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
return false;
}
</script>
</section>
</body>
&#13;