简体   繁体   中英

How to get price instead of data-price/value in Javascript?

What the title says... How can I grab price="".

In my HTML below, this is in Magento, which I'm trying to override by defining a custom Javascript. That's why I did not post it to Magento Stack Overflow. The option inside select tag is like this:

<option id="sample" value="46" price="2">50 x 46cm +$2.00</option>

How can I grab the price?

$('select').on('change', function() {
  var a = $(this).data(price);
  alert( a );
});

You need the attribute from the selected child

$('select').on('change', function() {
      var a = $(this).find(':selected').attr('price');
      alert( a );
});

Assumes that the <select> is not set as multiple . Also more common to use html5 data- attributes but should work fine as is

$('select').on('change', function() {
     var selected = $( "select option:selected " ).attr('price');
     console.log(selected);
});

OR if you have

$('.sample_list').on('change', function() {
    var selected = $( ".sample_list option:selected " ).attr('price');
    console.log(selected);
});

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