[英]jQuery get html5 data value on change
I've a form with a quantity select dropdown and a buy now button. 我有一个带有数量选择下拉列表和立即购买按钮的表格。
<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>
Now if a user select quantity 5 and click the buy now button it will display message like 现在,如果用户选择数量5并单击立即购买按钮,它将显示如下消息
{product title} 5X has been added to your order
Its working correctly. 它的工作正常。 But when the user select another quantity (say 7 ) and click buy now its supposed to display message like
但是,当用户选择其他数量(例如7)并单击“立即购买”时,其应该显示如下消息
{product title} 7X has been added to your order
But it still showing the same {product title} 5X has been added to your order
message. 但是它仍然显示相同的
{product title} 5X has been added to your order
消息中。
Can someone tell me whats wrong. 有人可以告诉我怎么了。
This is my jQuery code. 这是我的jQuery代码。
var quantity = $(this).data("quantity");
$('span.itemcount').replaceWith(""+ quantity +" X");
$(this).data("quantity")
始终来自静态data-quantity="5"
,其中this
是标签A
$(this).data("quantity")
$(this).data(“数量”)
will get data from the anchor 将从锚点获取数据
A
一种
tag which will be always 5. you need to get the value from 标记将始终为5。您需要从中获取值
<input type="number" step="1" name="quantity" ... />
You can do that as follows 您可以按照以下步骤进行操作
$(".input-text").value
Here is the code which updates the anchor code from your given link 这是从您给定链接更新锚代码的代码
// 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");
});
});
I fixed it. 我修好了它。 It works when I use like
当我使用像
var quantityid = $(this).attr('id');
var quantity = $("#" + quantityid + "").attr("data-quantity");
$('span.itemcount').replaceWith(""+ quantity +" X");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.