【问题标题】:get each value of the same data-attribute获取相同数据属性的每个值
【发布时间】:2018-02-25 02:33:40
【问题描述】:

我需要获取每个数据属性中的值并将该值分配给一个变量,然后我想将该变量作为参数传递给函数(我没有编写函数或 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 相同的值。为什么会这样?如何获得每个数据属性的值?请告诉我我做错了什么以及如何纠正。谢谢。

.

【问题讨论】:

    标签: javascript jquery function parameter-passing custom-data-attribute


    【解决方案1】:

    不知道您期望什么,但我认为您应该省略参数并在 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)
    }
    

    【讨论】:

    • 这看起来很有趣,因为我还必须传递其他参数才能获取它们的值(不是硬编码值)。我要试试这个。
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多