簡體   English   中英

獲取相同數據屬性的每個值

[英]get each value of the same data-attribute

我需要獲取每個數據屬性中的值,並將該值分配給一個變量,然后我想將該變量作為函數中的參數傳遞(我沒有寫函數或HTML,但必須使用它)。 數據屬性值是根據來自不同函數的表的行和列動態生成的。

我以為我可以做這樣的事情:

priceIndex = $(this).attr("data-priceIndex");

$(this)是父tableCell。

然后將priceIndex變量傳遞給函數,即

<tr><td class="tblProduct selectProduct" data-priceIndex="0_0">
    <div class="priceArea" data-priceIndex="0_0">
        <button class="okButton origProduct" onclick="goToCart(priceIndex, '', true, false)"></button>
    </div>
</td>
<td class="tblProduct selectProduct" data-priceIndex="0_1">
    <div class="priceArea" data-priceIndex="0_1">
        <button class="upgradeButton upgrdProduct" onclick="goToCart(priceIndex, '', false, false)"></button>
    </div>
</td></tr>

但這為所有data-priceIndex賦予了相同的值。 為什么會這樣呢? 如何獲得每個數據屬性的值? 請告訴我我在做什么錯以及如何糾正。 謝謝。

不知道您期望什么,但是我認為您應該忽略該參數並在goToCart函數中獲取值或“包裝”它,無論您單擊什么按鈕,嘗試priceIndex的方式始終具有相同的值:

<button class="upgradeButton upgrdProduct" onclick="goToCartWrap('',true,false)"></button>

和功能:

function goToCartWrap(s,b1,b2){
 priceIndex = $(this).parent().attr("data-priceIndex");
 goToCart(priceIndex, s, b1, b2)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM