亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Jquery在按鈕單擊頁面上的多個div中的div中以div的形式獲取數據

如何使用Jquery在按鈕單擊頁面上的多個div中的div中以div的形式獲取數據

慕田峪7331174 2021-05-19 17:13:32
我正在顯示來自div的數據庫中的數據。顯示的div的數量取決于數據庫記錄。每個div都有一個表單,其中包含隱藏的輸入和Submit按鈕。當我單擊Submit按鈕時,我應該只從該div的隱藏輸入中獲取數據。問題是我僅從第一個div獲取數據。HTML<div class="col-md-4 col-sm-4" th:each="product : ${products}">        <!--card -->        <div class="card" style="margin-bottom: 10px;">            <span th:if="${products.empty}" style="color: #0e4377">No Products Available</span>            <!-- Card image -->            <div class="view overlay">                <img class="card-img-top"  alt="productImage" th:attr="src=${product.image}" height="100px" width="50px" >            </div>            <!-- Card image -->            <div class="card-body" >                <!-- Title -->                <h4 class="card-title" th:text="${product.name}"></h4>                <!-- Text -->                <span>                    <i class="fas fa-dollar-sign"></i>                    <p class="card-text" th:text="${product.price}">$</p>                </span>                <!--button container-->                <div class="button-container" >                    <!-- Button -->                <form  action="#" th:object="${Product}" method="POST" >                    <input type="hidden" th:value="${product.id}" name="id" class="id"/>                    <input type="hidden" th:value="${product.name}" name="name" class="name"/>                    <input type="hidden" th:value="${product.image}" name="image" class="image"/>                    <input type="hidden" th:value="${product.price}" name="price" class="price"/>                    <input type="hidden"  th:value="${session.userId}" name="userId" class="userId"/>                <button type="submit" class="btn btn-info btn-sm" onclick="addToCart();">Add</button>                </form>                </div>                <!--button container-->            </div>        </div>
查看完整描述

2 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

嘗試傳遞關鍵字this

<button type="submit" class="btn btn-info btn-sm" onclick="addToCart(this);">Add</button>

function addToCart(this){
    var id = $(this).siblings(".id").val();


查看完整回答
反對 回復 2021-05-27
  • 2 回答
  • 0 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號