代码之家  ›  专栏  ›  技术社区  ›  Assaf

javascript-添加到购物车功能在尝试添加产品数量时不起作用

  •  -1
  • Assaf  · 技术社区  · 6 年前

    下面是java脚本代码,以及相关的html- 问题是-函数只返回第一个按钮的值,而不返回相关按钮的值。

    下面的代码只返回表的第一个值。当客户选择第二个产品时,代码不返回任何内容,或者再次返回第一个产品。

    我附上了屏幕截图和它的样子。抱歉,代码太长了,如果我没有包括所有的css,屏幕看起来就不一样了。 我想工作的那一排是169号 .

        <!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 .proceed {
                float: right;
                display: block;
                color: white;
                background-color: #2196F3;
                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>
    <div class="topnav">
        <a class="proceed" href="/cart">Proceed to checkout</a></div>
    
    <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}" class="product">
            <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><input type="text" placeholder="insert quantity" id="quantity" required></td>
            <td><button onclick="myfunction(this)" class="${product.productId}" type="button">Add to cart</button></td>
            <td><button onclick="myfunction1(this)" class="${product.productId}" type="button">Delete from cart</button></td></tr>
    </table>
        <script type="text/javascript">
            function getCookie(cname) {
                var name = cname + "=";
                var decodedCookie = decodeURIComponent(document.cookie);
                var ca = decodedCookie.split(';');
                for(var i = 0; i <ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') {
                        c = c.substring(1);
                    }
                    if (c.indexOf(name) == 0) {
                        return c.substring(name.length, c.length);
                    }
                }
                return "";
            }
    
            function myfunction(btn) {
                var quan = document.getElementById("quantity").value;
                var productid = btn.closest(".product").innerText.split("\t")[0];
                var clientid = getCookie("clientidcookie");
                var xhttp = new XMLHttpRequest();
                xhttp.open("GET","/products_log" + '?user=' + clientid + '&product=' + productid + '&quant=' + quan,true);
                xhttp.send();
                alert("Product added to cart");
            }
            function myfunction1(btn) {
                var productid = btn.closest(".product").innerText.split("\t")[0];
                var clientid = getCookie("clientidcookie");
                var xhttp = new XMLHttpRequest();
                xhttp.open("GET","/delete_log" + '?user=' + clientid + '&product=' + productid,true);
                xhttp.send();
            }
        </script>
    </body>
    </html>
    

    ‏var quan = btn.closest("tr").getElementsByClassName("quantity")[0].value;‏
    

    而不是:

                    var quan = document.getElementById("quantity").value;
    

    search-results

    1 回复  |  直到 6 年前
        1
  •  1
  •   Programmer    6 年前

    元素的Id应该是唯一的,所以当您使用 document.getElementById(...)

    你可以这样做-

    var quan = this.closest('tr').getElementById("quantity").value;