简体   繁体   English

获取相同数据属性的每个值

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

I need to get the value in each data-attribute and assign that value to a variable and then I want to pass that variable as an argument in a function (I didn't write the function or the html but have to use it). 我需要获取每个数据属性中的值,并将该值分配给一个变量,然后我想将该变量作为函数中的参数传递(我没有写函数或HTML,但必须使用它)。 The data-attribute value is dynamically generated based on the row and column of the table from a different function. 数据属性值是根据来自不同函数的表的行和列动态生成的。

I thought I could do something like this: 我以为我可以做这样的事情:

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

$(this) is the parent tableCell. $(this)是父tableCell。

And then pass the priceIndex variable to the function ie 然后将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>

But that gives me the same value for all data-priceIndex . 但这为所有data-priceIndex赋予了相同的值。 Why is that happening? 为什么会这样呢? How can I get the value of each data-attributes? 如何获得每个数据属性的值? Please tell me what I am doing wrong and how to correct. 请告诉我我在做什么错以及如何纠正。 Thanks. 谢谢。

.

Dont know what you expect but i think you should rather omit the parameter and retrieve the value in the goToCart function or "wrap" it, the way you try priceIndex will always have the same value no matter what button you click: 不知道您期望什么,但是我认为您应该忽略该参数并在goToCart函数中获取值或“包装”它,无论您单击什么按钮,尝试priceIndex的方式始终具有相同的值:

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

and the function: 和功能:

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