gpt4 book ai didi

javascript - Nodejs javascript 添加到按钮没有响应

转载 作者:行者123 更新时间:2023-12-02 23:07:08 24 4
gpt4 key购买 nike

表中的每个数据库条目都有一个按钮。由于表格不能在表格中,我无法使用表格提出请求。我向每个按钮添加了一小部分 javascript,该按钮应该使用数据库 ID 发出获取请求并重定向到不同的页面。它似乎不起作用。我测试了创建一个虚拟表单并在页面上发出相同的获取请求并且有效。有什么想法为什么这不起作用吗?

Nodejs 文件路由器

const express = require('express')
const router = express.Router()

router.get('/testRedirect/:id', (req, res) => res.render('pages/about'))

module.exports = router
<小时/>
<!DOCTYPE html>
<html>

<head>
<% include ../partials/header.ejs %>
</head>

<body>
<% include ../partials/nav.ejs %>

<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<% results.forEach(function(r) { %>
<tr>
<th scope="row">
<%= r.id %>
</th>
<th scope="row">
<%= r.name %>
</th>
<td>
<button type="submit" id=<%=r.quiz_id %> class="btn btn-primary">Edit</button>
<script type="text/javascript">
$(function() {
var id = <%=r.id %>
var idString = "#" + id
$(idString).click(function() {
$.get('/testRedirect/' + id)
})
});
</script>
</td>
<td>
<button type="submit" id=<%=r.Id %> class="btn btn-primary">Delete</button>
<script type="text/javascript">
</script>
</td>
</tr>
<% }); %>
</tbody>
</table>
</div>

<form action="/testRedirect/1" method="GET">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>

</html>

最佳答案

您真的需要每个按钮都有一个脚本 block 吗?不能使用 onclick 属性吗:

<button type="button" id=<%=r.quiz_id %> onclick="location.href='/testRedirect/#<%=r.quiz_id %>'" class="btn btn-primary">Edit</button>

关于javascript - Nodejs javascript 添加到按钮没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57535129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com