如何为简单的 crud 应用程序创建编辑功能? - 使用 VANILLA JAVASCRIPT 进行 CRUD 操作
How do I create an edit function for a simple crud-application? - CRUD operations with VANILLA JAVASCRIPT
所以我只使用 JavaScript 创建了一个简单的 CRUD 应用程序。
现在您可以将国家/地区添加到数组中并从数组中删除国家/地区。
我希望能够编辑数组中的现有国家/地区,例如我想将 "Stockholm" 更改为 "Spain"。
编辑功能会是什么样子?
下面是我当前的代码,html 和 javascript。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Städer</h1>
<div id="output"></div>
<div class="submit">
<input type="text" id="staden" placeholder="Ny stad"/>
<button onclick="laggaTill()" id="btnClick">Lägg till</button>
</div>
<script src="checkpoint1.js"></script>
</div>
</body>
</html>
var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();
function uppdateraOutput(){
var output = "";
for (var i = 0; i < stader.length; i ++) {
output += stader[i] + "[<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>]<br/>";
}
document.getElementById("output").innerHTML = output;
}
function laggaTill() {
console.log("Lägg till");
var stad = document.getElementById("staden").value;
if (stad.length != 0) {
stader[stader.length] = stad;
document.getElementById("staden").value = "";
uppdateraOutput();
}
}
function taBort(id) {
console.log("Ta bort: " + id);
var staderTemp = [];
for (var i = 0; i < stader.length; i++){
if (i !=id) {
staderTemp.push(stader[i]);
}
}
stader = staderTemp;
uppdateraOutput();
}
function edit() {
}
您可以为每个项目添加点击功能,当您点击文本时,它会打开提示 window 以更改文本。
我更改了 uppdateraOutput 函数以向文本添加点击功能。
<span onclick='edit("+ i +")'>" + stader[i] + "</span>
喜欢下面的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Städer</h1>
<div id="output"></div>
<div class="submit">
<input type="text" id="staden" placeholder="Ny stad"/>
<button onclick="laggaTill()" id="btnClick">Lägg till</button>
</div>
<script src="checkpoint1.js"></script>
</div>
<script>
var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();
function uppdateraOutput(){
var output = "";
for (var i = 0; i < stader.length; i ++) {
output += "<span onclick='edit("+ i +")'>" + stader[i] + "</span> [<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>] <br/>";
}
document.getElementById("output").innerHTML = output;
}
function laggaTill() {
console.log("Lägg till");
var stad = document.getElementById("staden").value;
if (stad.length != 0) {
stader[stader.length] = stad;
document.getElementById("staden").value = "";
uppdateraOutput();
}
}
function taBort(id) {
console.log("Ta bort: " + id);
var staderTemp = [];
for (var i = 0; i < stader.length; i++){
if (i !=id) {
staderTemp.push(stader[i]);
}
}
stader = staderTemp;
uppdateraOutput();
}
function edit(index) {
var item = prompt("Please enter your name", stader[index]);
if (item !== null && item !== "") {
stader[index] = item;
}
uppdateraOutput();
}
</script>
</body>
</html>
我发现了两个问题。
- 使用文本操作 document/html。这很难维护。学的时候还好,不学就不行
- 循环遍历数组以进行每次更改、添加和编辑
我给了你一个不同的方法,没有执行上面提到的两个问题。
var clickedItem = null;
function clickListener(e) {
document.getElementById('staden').value = e.target.id
clickedItem = e.target
}
function laggaTill() {
var ul = document.getElementById("list")
var stad = document.getElementById("staden").value;
var li = document.createElement("li")
li.id = stad
li.addEventListener("click", clickListener)
li.appendChild(document.createTextNode(stad))
ul.appendChild(li)
document.getElementById("staden").value = ""
}
function edit() {
var item = document.getElementById(clickedItem.id)
var stad = document.getElementById('staden').value
item.id=stad
item.innerHTML = stad
clickedItem = null
document.getElementById("staden").value = ""
}
所以我只使用 JavaScript 创建了一个简单的 CRUD 应用程序。 现在您可以将国家/地区添加到数组中并从数组中删除国家/地区。 我希望能够编辑数组中的现有国家/地区,例如我想将 "Stockholm" 更改为 "Spain"。
编辑功能会是什么样子? 下面是我当前的代码,html 和 javascript。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Städer</h1>
<div id="output"></div>
<div class="submit">
<input type="text" id="staden" placeholder="Ny stad"/>
<button onclick="laggaTill()" id="btnClick">Lägg till</button>
</div>
<script src="checkpoint1.js"></script>
</div>
</body>
</html>
var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();
function uppdateraOutput(){
var output = "";
for (var i = 0; i < stader.length; i ++) {
output += stader[i] + "[<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>]<br/>";
}
document.getElementById("output").innerHTML = output;
}
function laggaTill() {
console.log("Lägg till");
var stad = document.getElementById("staden").value;
if (stad.length != 0) {
stader[stader.length] = stad;
document.getElementById("staden").value = "";
uppdateraOutput();
}
}
function taBort(id) {
console.log("Ta bort: " + id);
var staderTemp = [];
for (var i = 0; i < stader.length; i++){
if (i !=id) {
staderTemp.push(stader[i]);
}
}
stader = staderTemp;
uppdateraOutput();
}
function edit() {
}
您可以为每个项目添加点击功能,当您点击文本时,它会打开提示 window 以更改文本。
我更改了 uppdateraOutput 函数以向文本添加点击功能。
<span onclick='edit("+ i +")'>" + stader[i] + "</span>
喜欢下面的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Städer</h1>
<div id="output"></div>
<div class="submit">
<input type="text" id="staden" placeholder="Ny stad"/>
<button onclick="laggaTill()" id="btnClick">Lägg till</button>
</div>
<script src="checkpoint1.js"></script>
</div>
<script>
var stader = ["Stockholm", "Köpenhamn", "Paris"];
uppdateraOutput();
function uppdateraOutput(){
var output = "";
for (var i = 0; i < stader.length; i ++) {
output += "<span onclick='edit("+ i +")'>" + stader[i] + "</span> [<span title = 'Ta bort " + stader[i] + "' onclick= 'taBort("+ i +")'> x </span>] <br/>";
}
document.getElementById("output").innerHTML = output;
}
function laggaTill() {
console.log("Lägg till");
var stad = document.getElementById("staden").value;
if (stad.length != 0) {
stader[stader.length] = stad;
document.getElementById("staden").value = "";
uppdateraOutput();
}
}
function taBort(id) {
console.log("Ta bort: " + id);
var staderTemp = [];
for (var i = 0; i < stader.length; i++){
if (i !=id) {
staderTemp.push(stader[i]);
}
}
stader = staderTemp;
uppdateraOutput();
}
function edit(index) {
var item = prompt("Please enter your name", stader[index]);
if (item !== null && item !== "") {
stader[index] = item;
}
uppdateraOutput();
}
</script>
</body>
</html>
我发现了两个问题。
- 使用文本操作 document/html。这很难维护。学的时候还好,不学就不行
- 循环遍历数组以进行每次更改、添加和编辑
我给了你一个不同的方法,没有执行上面提到的两个问题。
var clickedItem = null;
function clickListener(e) {
document.getElementById('staden').value = e.target.id
clickedItem = e.target
}
function laggaTill() {
var ul = document.getElementById("list")
var stad = document.getElementById("staden").value;
var li = document.createElement("li")
li.id = stad
li.addEventListener("click", clickListener)
li.appendChild(document.createTextNode(stad))
ul.appendChild(li)
document.getElementById("staden").value = ""
}
function edit() {
var item = document.getElementById(clickedItem.id)
var stad = document.getElementById('staden').value
item.id=stad
item.innerHTML = stad
clickedItem = null
document.getElementById("staden").value = ""
}