简体   繁体   中英

Save html5 data attribute value in a variable

I have link like this

<a href="#" data-quantity="1" class="buynow">Buy now </a>

data-quantity value is assigned when the user select product quantity.

As of now I getting data-quantity value like this

jQuery(document).ready(function($){ 
    $(document).on( 'click', '.buynow', function() { 
        var quantity = $(this).attr('data-quantity');
    });
});

The code works fine at first, but if the user changes the quantity again, then it displays the previously used value.

Can some tell me how to refresh the value?

Thanks

Update:

My quantity select button looks like this

<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>

You need some code to change the data tag

$('.minus').click(function(){
  var qty = $('.buynow').data('quantity');
  if(qty!=0)
    $('.buynow').data('quantity', qty - 1);
})
$('.plus').click(function(){
  $('.buynow').data('quantity', $('.buynow').data('quantity') + 1);
})

Not tested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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