我有一些資料集包括加密硬幣名稱、符號等。當我單擊按鈕時,我希望在控制臺中看到硬幣符號。第一個是好的,但其他人沒有作業。我看空。單擊詳細資訊時,我想查看具有名稱或符號的所有匹配資料。我的錯誤在哪里?
我的ejs
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<table style="width:850px; margin:15px; text-align: center;">
<caption>
<h1>Coins</h1>
</caption>
<thead style="font-size: 20px;">
<tr style="border:2px solid">
<td>Name</td>
<td>Slug</td>
</tr>
</thead>
<tbody>
<% for (i=0; i<100; i ){ %>
<tr>
<td> <%= cName[i] %> </td>
<td> <%= symbol[i] %> </td>
<td>
<form method="post" id="form" action="/">
<input id="hiddeninput" name="hiddeninput" type="hidden" />
<button id="coinName" value="<%= symbol[i] %>" name="buttonname">Detail</button> </td> //Here comes from Mongo like BTC,ETH..
</form>
//and some td's
</tr>
<% } %>
</tbody>
<tfoot>
</tfoot>
</table>
<script>
$('#coinName').on('click', function(){
$('#hiddeninput').val($(this).val());
});
</script>
</body>
</html>
Ejs 看起來像
| 姓名 | 蛞蝓 | 細節 |
|---|---|---|
| 位元幣 | 位元幣 | 詳細資訊按鈕 |
| 以太坊 | 以太坊 | 詳細資訊按鈕 |
服務器.js
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
const name = [];
const symbol = [];
const price = [];
const date = [];
app.get("/", (req, res) => {
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("cryptoDb");
dbo.collection("coinTable").find({}).toArray(function (err, finds) {
if (err) throw err;
for (i = 0; i < finds[0].result.length; i ) {
name.push(finds[0].result[i].name);
symbol.push(finds[0].result[i].symbol);
price.push(finds[0].result[i].price);
date.push(finds[0].result[i].date);
}
res.render('index', {
cName: name,
symbol: symbol,
len: finds[0].result.length,
cPrice:price,
cDate:date
});
db.close();
});
});
})
app.post('/', (req, res) => {
console.log(req.body.hiddeninput);
//Here is my function, fetch all datas from Mongo where matches hiddeninput
})
var server = app.listen(3000, function () {
console.log(`port: ${server.address().port}`);
}
)
提前非常感謝!
uj5u.com熱心網友回復:
ID 必須是唯一的。只要有一個表格。提交按鈕名稱和值將提交被點擊的按鈕
不需要腳本
<form method="post" id="form" action="/">
<table style="width:850px; margin:15px; text-align: center;">
<caption>
<h1>Coins</h1>
</caption>
<thead style="font-size: 20px;">
<tr style="border:2px solid">
<td>Name</td>
<td>Slug</td>
</tr>
</thead>
<tbody>
<% for (i=0; i<100; i ){ %>
<tr>
<td>
<%= cName[i] %>
</td>
<td>
<%= symbol[i] %>
</td>
<td>
<button name="coinName" value="<%= symbol[i] %>">Detail</button> </td> //Here comes from Mongo like BTC,ETH..
</tr>
<% } %>
</tbody>
<tfoot>
</tfoot>
</table>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/439207.html
標籤:javascript jQuery 节点.js 表示 ejs
