简体   繁体   English

如何在 jQuery 中保存数据属性的原始值

[英]How can I save the original value of a data attribute in jQuery

I have an element that I'm using with a quantity input.我有一个与数量输入一起使用的元素。 When the quantity changes I update the data attribute with the new value.当数量发生变化时,我用新值更新数据属性。

The problem is that when the data attribute is changed, the next iteration of the change is being based off the changed value not the original value.问题是,当数据属性发生变化时,下一次变化迭代是基于变化的值而不是原始值。

 $(".inc").click(function(){ service = $(this).closest(".service-option-card"); let quantity = getCurrentQuantity(service); let newQuantity = quantity+1; setNewQuantity(newQuantity, service); updatePrice(newQuantity, service); }); $(".dec").click(function(){ service = $(this).closest(".service-option-card"); let quantity = getCurrentQuantity(service); let newQuantity = quantity-1; if(newQuantity <= 0) { let newQuantity = 0 setNewQuantity(newQuantity, service); } else { setNewQuantity(newQuantity, service); } updatePrice(newQuantity, $(this).closest(".service-option-card")); }); function getCurrentQuantity(service) { let quantity_str = service.find(".quantity").val(); quantity_num = Number(quantity_str); return quantity_num; } function setNewQuantity(quantity, service) { service.find(".quantity").val(quantity); } function updatePrice(quantity, service) { var price = parseInt(service.find(".price").val().replace(/,/g, "")); var total = quantity * price; if(total < 0) { total = 0; } newPrice = numberFormat(total); service.find(".option-price").html("$" + newPrice); var monthly = service.data('monthly'); newMonthly = monthly * quantity; console.log(newMonthly); updateReceiptMonthly(service, newMonthly); } function updateReceiptMonthly(service, newMonthly) { service.data('monthly', newMonthly); } // Number format function numberFormat(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1? '.' + x[1]: ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="service-option-card" data-monthly="10"> <input style="display: none;" class="price" value="50"> <p class="option-price">$50</p> <div class="quantity"> <button class="btn btn-quantity dec">-</button> <input class="quantity-input quantity" value="1"> <button class="btn btn-quantity inc">+</button> </div> </div> <div class="col-md-6 services-total-side"> <div class="calculated-totals"> <p class="cost-title">Monthly Cost:</p> <p class="monthly-cost">$0</p> </div> </div>

When you run the above snippet, you can see the console.log is incrementing 10 > 20 > 60 > 240 and so on.当你运行上面的代码片段时,你可以看到console.log正在递增 10 > 20 > 60 > 240 等等。 this is because when I update the data-monthly attribute on my service-option-card its then multiplying the NEW value by the quantity.这是因为当我更新service-option-card上的data-monthly属性时,它会将 NEW 值乘以数量。 What I would like is for it to multiply by the ORIGINAL value of 10 so the console.log should be 10 > 20 > 30 > 40 and so on.我想要的是它乘以原始值 10,所以console.log应该是 10 > 20 > 30 > 40 等等。 I cant just put 10 in because the values change in my application for diffrent service option cards.我不能只输入 10,因为我的应用程序中不同服务选项卡的值会发生变化。

You could add another property with the same value, but then keep this value the same.您可以添加另一个具有相同值的属性,然后保持该值相同。 Then you'll always have the original base value to use:然后,您将始终使用原始基值:

 $(".inc").click(function(){ service = $(this).closest(".service-option-card"); let quantity = getCurrentQuantity(service); let newQuantity = quantity+1; setNewQuantity(newQuantity, service); updatePrice(newQuantity, service); }); $(".dec").click(function(){ service = $(this).closest(".service-option-card"); let quantity = getCurrentQuantity(service); let newQuantity = quantity-1; if(newQuantity <= 0) { let newQuantity = 0 setNewQuantity(newQuantity, service); } else { setNewQuantity(newQuantity, service); } updatePrice(newQuantity, $(this).closest(".service-option-card")); }); function getCurrentQuantity(service) { let quantity_str = service.find(".quantity").val(); quantity_num = Number(quantity_str); return quantity_num; } function setNewQuantity(quantity, service) { service.find(".quantity").val(quantity); } function updatePrice(quantity, service) { var price = parseInt(service.find(".price").val().replace(/,/g, "")); var total = quantity * price; if(total < 0) { total = 0; } newPrice = numberFormat(total); service.find(".option-price").html("$" + newPrice); // now using original value here var monthly = service.data('base-monthly'); newMonthly = monthly * quantity; console.log(newMonthly); updateReceiptMonthly(service, newMonthly); } function updateReceiptMonthly(service, newMonthly) { service.data('monthly', newMonthly); } // Number format function numberFormat(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1? '.' + x[1]: ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="service-option-card" data-monthly="10" data-base-monthly="10"> <input style="display: none;" class="price" value="50"> <p class="option-price">$50</p> <div class="quantity"> <button class="btn btn-quantity dec">-</button> <input class="quantity-input quantity" value="1"> <button class="btn btn-quantity inc">+</button> </div> </div> <div class="col-md-6 services-total-side"> <div class="calculated-totals"> <p class="cost-title">Monthly Cost:</p> <p class="monthly-cost">$0</p> </div> </div>

did you try to create another data-monthly attribute that does not change?您是否尝试创建另一个不变的数据月度属性? for example call it data-monthly-base and use it in updatePrice function instead of data-monthly?例如,将其称为 data-monthly-base 并在 updatePrice function 中使用它而不是 data-monthly?

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

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