获取相同数据属性的每个值
问题描述:
我需要获取每个数据属性中的值并将该值分配给一个变量,然后我想将该变量作为函数中的参数传递(我没有写入该函数或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)
}
这看起来很有趣,因为我必须通过其他参数以及获得它们的值(不是硬编码值)。我要试试这个。 – Chris22