繁体   English   中英

jQuery更改时获取html5数据值

[英]jQuery get html5 data value on change

我有一个带有数量选择下拉列表和立即购买按钮的表格。

<a href="#" rel="nofollow" data-product_id="75" data-product_sku="75" data-popup_id="popup_content_75" data-quantity="5" class="add_to_cart_button_popup button alt">Buy Now</a>
<div class="quantity buttons_added">
<input type="button" value="-" class="minus">
<input type="number" step="1" name="quantity" value="1" title="Qty" class="input-text qty text">
<input type="button" value="+" class="plus"></div> 

现在,如果用户选择数量5并单击立即购买按钮,它将显示如下消息

{product title} 5X has been added to your order

它的工作正常。 但是,当用户选择其他数量(例如7)并单击“立即购买”时,其应该显示如下消息

{product title} 7X has been added to your order

但是它仍然显示相同的{product title} 5X has been added to your order消息中。

有人可以告诉我怎么了。

这是我的jQuery代码。

 var quantity = $(this).data("quantity");
 $('span.itemcount').replaceWith(""+ quantity +" X");

$(this).data("quantity")始终来自静态data-quantity="5" ,其中this是标签A

$(this).data(“数量”)

将从锚点获取数据

一种

标记将始终为5。您需要从中获取值

<input type="number" step="1" name="quantity" ... />

您可以按照以下步骤进行操作

$(".input-text").value

这是从您给定链接更新锚代码的代码

// JavaScript source code
jQuery(document).ready(function (e) {
    e(document).on("click", ".plus, .minus", function () {
        var t = e(this).closest(".quantity").find(".qty"),
            n = parseFloat(t.val()),
            r = parseFloat(t.attr("max")),
            i = parseFloat(t.attr("min")),
            s = t.attr("step");
        if (!n || n == "" || n == "NaN") n = 0;
        if (r == "" || r == "NaN") r = "";
        if (i == "" || i == "NaN") i = 0;
        if (s == "any" || s == "" || s == undefined || parseFloat(s) == "NaN") s = 1;
        e(this).is(".plus")
            ? r && (r == n || n > r) ? t.val(r) : t.val(n + parseFloat(s))
            : i && (i == n || n < i) ? t.val(i) : n > 0 && t.val(n - parseFloat(s));
        t.trigger("change");

    });
});

我修好了它。 当我使用像

 var quantityid = $(this).attr('id');
 var quantity = $("#" + quantityid + "").attr("data-quantity");
 $('span.itemcount').replaceWith(""+ quantity +" X");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM