我的目标 - 单击按钮,获取产品值(value),添加到购物车。
我有一个 html 页面,我想在其中单击按钮并获取 ProductId 值,以便获取它,将其插入购物车。 每次加载页面时都会从数据库中获取我希望能够添加到购物车的记录
问题是,当我按下按钮时,即使我将其与 ID 连接,我也只获得第一条记录,而且按钮似乎根本没有连接到表。
代码如下:
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {box-sizing: border-box;}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #e9e9e9;
}
.topnav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #2196F3;
color: white;
}
.topnav .search-container {
float: right;
}
.topnav input[type=text] {
padding: 6px;
margin-top: 8px;
font-size: 17px;
border: none;
}
.topnav .search-container button {
float: right;
padding: 6px 10px;
margin-top: 8px;
margin-right: 16px;
background: #ddd;
font-size: 17px;
border: none;
cursor: pointer;
}
.topnav .search-container button:hover {
background: #ccc;
}
@media screen and (max-width: 600px) {
.topnav .search-container {
float: none;
}
.topnav a, .topnav input[type=text], .topnav .search-container button {
float: none;
display: block;
text-align: left;
width: 100%;
margin: 0;
padding: 14px;
}
.topnav input[type=text] {
border: 1px solid #ccc;
}
}
</style>
</head>
<div class="topnav">
<a class="active" href="/search">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<div class="search-container">
<form action="/results">
<input type="text" placeholder="Search.." name="search">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
<div style="padding-left:16px">
</div>
<style>
table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
</style>
</head>
<body>
<h2>Search Results</h2>
<table id="products">
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Brand</th>
<th>Rating</th>
<th>Price</th>
<th>Availability</th>
<tr th:each="product : ${results}">
<td th:text="${product.productId}" id="productId"></td>
<td th:text="${product.productName}"></td>
<td th:text="${product.brand}"></td>
<td th:text="${product.rating}"></td>
<td th:text="${product.price}"></td>
<td th:text="${product.availableInStock}"></td>
<td><button onclick="myfunction()" class="btn-select" type="button" id="buttonc">Add to cart</button></td>
</tr>
</table>
<script>
function myfunction() {
alert(document.getElementById("productId").outerText);
}
</script>
</body>
</html>
最佳答案
您的所有按钮都将调用相同的功能,使您无法确定应该将哪个产品添加到购物车。您应该为按钮提供一个数据属性,例如 data-productid
并将其作为参数传递给函数 ( this.dataset.productid
) 以获取应添加到购物车的产品的 ID。您还应该为表单元格之一提供产品 ID 的 ID,并使用 document.getElementById(productid).parentNode
获取该行。 .
此外,您的 HTML 存在一些语法错误,例如正文和两端之前有 HTML(标题)</head>
标签。
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {box-sizing: border-box;}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #e9e9e9;
}
.topnav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #2196F3;
color: white;
}
.topnav .search-container {
float: right;
}
.topnav input[type=text] {
padding: 6px;
margin-top: 8px;
font-size: 17px;
border: none;
}
.topnav .search-container button {
float: right;
padding: 6px 10px;
margin-top: 8px;
margin-right: 16px;
background: #ddd;
font-size: 17px;
border: none;
cursor: pointer;
}
.topnav .search-container button:hover {
background: #ccc;
}
@media screen and (max-width: 600px) {
.topnav .search-container {
float: none;
}
.topnav a, .topnav input[type=text], .topnav .search-container button {
float: none;
display: block;
text-align: left;
width: 100%;
margin: 0;
padding: 14px;
}
.topnav input[type=text] {
border: 1px solid #ccc;
}
}
table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
</style>
</head>
<body>
<div class="topnav">
<a class="active" href="/search">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<div class="search-container">
<form action="/results">
<input type="text" placeholder="Search.." name="search">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
<div style="padding-left:16px">
</div>
<h2>Search Results</h2>
<table id="products">
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Brand</th>
<th>Rating</th>
<th>Price</th>
<th>Availability</th>
<tr th:each="product : ${results}">
<td th:text="${product.productId}" id="${product.productId}"></td>
<td th:text="${product.productName}"></td>
<td th:text="${product.brand}"></td>
<td th:text="${product.rating}"></td>
<td th:text="${product.price}"></td>
<td th:text="${product.availableInStock}"></td>
<td><button onclick="myfunction(this.dataset.productid)" class="btn-select" type="button" data-productid="${product.productId}">Add to cart</button></td>
</tr>
</table>
<script>
function myfunction(productId) {
alert(productId);
var row = document.getElementById(productId).parentNode;
console.log(row);
}
</script>
</body>
</html>
关于javascript - HTML - 单击按钮获取表格单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716043/