嗨,我正在创建这个应用程序,然后我使用javascript,这样无论我在框中输入什么,我都可以在我的详细信息历史中看到它。我确实插入了单元格之类的东西,但是当我运行它时,我可以看到我的表数据正在被插入,但是仅仅几秒钟,它就会消失,我不知道为什么我希望您能帮助我<3。下面是我的代码
let enter = document.getElementById("btn");
enter.addEventListener("click", displayFunction);
let row = 1;
function displayFunction() {
let detail = document.getElementById("detail-input").value;
let amount = document.getElementById("amount-input").value;
if (!detail || !amount) {
alert("Please Fill in the Boxes");
return;
}
let table = document.getElementById("myTable");
let newRow = table.insertRow(row);
let cell1 = newRow.insertCell(0);
let cell2 = newRow.insertCell(1);
cell1.innerHTML = detail;
cell2.innerHTML = amount;
row++;
}@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins";
}
.container {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.685);
}
.form-container {
min-width: 500px;
min-height: 600px;
background: linear-gradient(
55deg,
rgb(71, 126, 247),
rgba(53, 52, 52, 0.295)
);
padding: 20px 30px;
border: 5px solid rgb(37, 37, 49);
border-radius: 10px;
text-align: center;
color: rgb(58, 57, 57);
box-shadow: 2px 2px 20px #333;
position: relative;
}
.form-container h1 {
margin-bottom: 2rem;
}
.form-container h3 {
margin-bottom: 2rem;
position: relative;
}
.form-container h3::before {
content: "";
width: 4rem;
height: 2px;
background-color: rgb(41, 32, 32);
position: absolute;
top: 50%;
left: 20%;
}
.form-container h3::after {
content: "";
width: 4rem;
height: 2px;
background-color: rgb(41, 32, 32);
position: absolute;
top: 50%;
right: 20%;
}
form {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
form input {
width: 100%;
height: 100%;
padding: 10px;
background: none;
outline: none;
border: none;
border-bottom: 2px solid rgb(41, 32, 32);
}
.detail {
margin-top: 20px;
}
.amount {
margin-top: 50px;
}
#btn {
text-decoration: none;
color: #000;
border: 1px solid;
border-radius: 20px;
background: #fff;
padding: 10px 20px;
position: absolute;
bottom: 28%;
cursor: pointer;
transition: 500ms ease-out;
}
#btn:hover {
background: #000;
color: #fff;
}
.history {
margin-top: 80px;
display: flex;
justify-content: space-around;
align-items: center;
}
.col-2 {
border: 1px solid;
padding: 5px 10px;
background: #fff;
}
.label-1 {
color: green;
}
.label-2 {
color: red;
}
.label-3 {
color: rgb(24, 166, 223);
}
.details-container {
margin-left: 10px;
width: 500px;
height: 600px;
border: 5px solid #333;
border-radius: 10px;
text-align: center;
background: linear-gradient(
55deg,
rgb(71, 126, 247),
rgba(53, 52, 52, 0.295)
);
}
.details-container h1 {
margin-top: 20px;
}
#myTable {
border-collapse: collapse;
background: #fff;
border: 1px solid;
width: 100%;
}
#myTable,
td,
th {
border: 2px solid;
padding: 10px 20px;
}
td {
color: #000;
}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Expenses Tracker App</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="form-container">
<h1>EXPENSES TRACKER APP</h1>
<h3>New Entry</h3>
<form id="form">
<div class="detail">
<div class="col-1">
<label>Details</label>
<input type="text" id="detail-input" />
</div>
</div>
<div class="amount">
<div class="col-1">
<label>Amount</label>
<input type="number" id="amount-input" />
</div>
</div>
<button id="btn">Enter</button>
</form>
<div class="history">
<div class="col-2 col-ie">
<p class="label-1">INFLOW</p>
<p id="inflow">$0.00</p>
</div>
<div class="col-2">
<p class="label-2">OUTFLOW</p>
<p id="outflow">$0.00</p>
</div>
<div class="col-2">
<p class="label-3">BALANCE</p>
<p id="balance">$0.00</p>
</div>
</div>
</div>
<div class="details-container">
<h1>DETAILS HISTORY</h1>
<table id="myTable">
<tr>
<th>Details</th>
<th>Amount</th>
</tr>
</table>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
发布于 2021-04-07 11:41:21
提交表单的preventDefault()
let enter = document.getElementById("form").addEventListener("submit", displayFunction);
function displayFunction(e) {
e.preventDefault(); <<<
let enter = document.getElementById("form").addEventListener("submit", displayFunction);
let row = 1;
function displayFunction(e) {
e.preventDefault();
let detail = document.getElementById("detail-input").value;
let amount = document.getElementById("amount-input").value;
if (!detail || !amount) {
alert("Please Fill in the Boxes");
return;
}
let table = document.getElementById("myTable");
let newRow = table.insertRow(row);
let cell1 = newRow.insertCell(0);
let cell2 = newRow.insertCell(1);
cell1.innerHTML = detail;
cell2.innerHTML = amount;
row++;
}@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins";
}
.container {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.685);
}
.form-container {
min-width: 500px;
min-height: 600px;
background: linear-gradient( 55deg, rgb(71, 126, 247), rgba(53, 52, 52, 0.295));
padding: 20px 30px;
border: 5px solid rgb(37, 37, 49);
border-radius: 10px;
text-align: center;
color: rgb(58, 57, 57);
box-shadow: 2px 2px 20px #333;
position: relative;
}
.form-container h1 {
margin-bottom: 2rem;
}
.form-container h3 {
margin-bottom: 2rem;
position: relative;
}
.form-container h3::before {
content: "";
width: 4rem;
height: 2px;
background-color: rgb(41, 32, 32);
position: absolute;
top: 50%;
left: 20%;
}
.form-container h3::after {
content: "";
width: 4rem;
height: 2px;
background-color: rgb(41, 32, 32);
position: absolute;
top: 50%;
right: 20%;
}
form {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
form input {
width: 100%;
height: 100%;
padding: 10px;
background: none;
outline: none;
border: none;
border-bottom: 2px solid rgb(41, 32, 32);
}
.detail {
margin-top: 20px;
}
.amount {
margin-top: 50px;
}
#btn {
text-decoration: none;
color: #000;
border: 1px solid;
border-radius: 20px;
background: #fff;
padding: 10px 20px;
position: absolute;
bottom: 28%;
cursor: pointer;
transition: 500ms ease-out;
}
#btn:hover {
background: #000;
color: #fff;
}
.history {
margin-top: 80px;
display: flex;
justify-content: space-around;
align-items: center;
}
.col-2 {
border: 1px solid;
padding: 5px 10px;
background: #fff;
}
.label-1 {
color: green;
}
.label-2 {
color: red;
}
.label-3 {
color: rgb(24, 166, 223);
}
.details-container {
margin-left: 10px;
width: 500px;
height: 600px;
border: 5px solid #333;
border-radius: 10px;
text-align: center;
background: linear-gradient( 55deg, rgb(71, 126, 247), rgba(53, 52, 52, 0.295));
}
.details-container h1 {
margin-top: 20px;
}
#myTable {
border-collapse: collapse;
background: #fff;
border: 1px solid;
width: 100%;
}
#myTable,
td,
th {
border: 2px solid;
padding: 10px 20px;
}
td {
color: #000;
}<div class="container">
<div class="form-container">
<h1>EXPENSES TRACKER APP</h1>
<h3>New Entry</h3>
<form id="form">
<div class="detail">
<div class="col-1">
<label>Details</label>
<input type="text" id="detail-input" />
</div>
</div>
<div class="amount">
<div class="col-1">
<label>Amount</label>
<input type="number" id="amount-input" />
</div>
</div>
<button id="btn">Enter</button>
</form>
<div class="history">
<div class="col-2 col-ie">
<p class="label-1">INFLOW</p>
<p id="inflow">$0.00</p>
</div>
<div class="col-2">
<p class="label-2">OUTFLOW</p>
<p id="outflow">$0.00</p>
</div>
<div class="col-2">
<p class="label-3">BALANCE</p>
<p id="balance">$0.00</p>
</div>
</div>
</div>
<div class="details-container">
<h1>DETAILS HISTORY</h1>
<table id="myTable">
<tr>
<th>Details</th>
<th>Amount</th>
</tr>
</table>
</div>
</div>
发布于 2021-04-07 11:59:09
let enter = document.getElementById("btn");
enter.addEventListener("click", displayFunction);
let row = 1;
function displayFunction() {
let detail = document.getElementById("detail-input").value;
let amount = document.getElementById("amount-input").value;
if (!detail || !amount) {
alert("Please Fill in the Boxes");
return;
}
let table = document.getElementById("myTable");
console.log(table)
let newRow = table.insertRow(row);
let cell1 = newRow.insertCell(0);
console.log(newRow);
let cell2 = newRow.insertCell(1);
cell1.innerHTML = detail;
cell2.innerHTML = amount;
row++;
return;
}@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins";
}
.container {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.685);
}
.form-container {
min-width: 500px;
min-height: 600px;
background: linear-gradient(
55deg,
rgb(71, 126, 247),
rgba(53, 52, 52, 0.295)
);
padding: 20px 30px;
border: 5px solid rgb(37, 37, 49);
border-radius: 10px;
text-align: center;
color: rgb(58, 57, 57);
box-shadow: 2px 2px 20px #333;
position: relative;
}
.form-container h1 {
margin-bottom: 2rem;
}
.form-container h3 {
margin-bottom: 2rem;
position: relative;
}
.form-container h3::before {
content: "";
width: 4rem;
height: 2px;
background-color: rgb(41, 32, 32);
position: absolute;
top: 50%;
left: 20%;
}
.form-container h3::after {
content: "";
width: 4rem;
height: 2px;
background-color: rgb(41, 32, 32);
position: absolute;
top: 50%;
right: 20%;
}
form {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
form input {
width: 100%;
height: 100%;
padding: 10px;
background: none;
outline: none;
border: none;
border-bottom: 2px solid rgb(41, 32, 32);
}
.detail {
margin-top: 20px;
}
.amount {
margin-top: 50px;
}
#btn {
text-decoration: none;
color: #000;
border: 1px solid;
border-radius: 20px;
background: #fff;
padding: 10px 20px;
position: absolute;
bottom: 28%;
cursor: pointer;
transition: 500ms ease-out;
}
#btn:hover {
background: #000;
color: #fff;
}
.history {
margin-top: 80px;
display: flex;
justify-content: space-around;
align-items: center;
}
.col-2 {
border: 1px solid;
padding: 5px 10px;
background: #fff;
}
.label-1 {
color: green;
}
.label-2 {
color: red;
}
.label-3 {
color: rgb(24, 166, 223);
}
.details-container {
margin-left: 10px;
width: 500px;
height: 600px;
border: 5px solid #333;
border-radius: 10px;
text-align: center;
background: linear-gradient(
55deg,
rgb(71, 126, 247),
rgba(53, 52, 52, 0.295)
);
}
.details-container h1 {
margin-top: 20px;
}
#myTable {
border-collapse: collapse;
background: #fff;
border: 1px solid;
width: 100%;
}
#myTable,
td,
th {
border: 2px solid;
padding: 10px 20px;
}
td {
color: #000;
}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Expenses Tracker App</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="form-container">
<h1>EXPENSES TRACKER APP</h1>
<h3>New Entry</h3>
<div id="form">
<div class="detail">
<div class="col-1">
<label>Details</label>
<input type="text" id="detail-input" />
</div>
</div>
<div class="amount">
<div class="col-1">
<label>Amount</label>
<input type="number" id="amount-input" />
</div>
</div>
</div>
<button id="btn">Enter</button>
<div class="history">
<div class="col-2 col-ie">
<p class="label-1">INFLOW</p>
<p id="inflow">$0.00</p>
</div>
<div class="col-2">
<p class="label-2">OUTFLOW</p>
<p id="outflow">$0.00</p>
</div>
<div class="col-2">
<p class="label-3">BALANCE</p>
<p id="balance">$0.00</p>
</div>
</div>
</div>
<div class="details-container">
<h1>DETAILS HISTORY</h1>
<table id="myTable">
<tr>
<th>Details</th>
<th>Amount</th>
</tr>
</table>
</div>
</div>
</body>
</html>
这是正确的,因为这里使用的是用div替换后的结果。https://jsfiddle.net/vishalpatil550/5xmu081o/7/
https://stackoverflow.com/questions/66985276
复制相似问题