Javascript 在更改下拉列表时获取href属性的值

Javascript 在更改下拉列表时获取href属性的值,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我有一个下拉列表,用户可以在其中选择不同的netweight。例如200ml、300ml等 <?php foreach($todays_offers as $offer) { $productname= $offer->product_name; $price= $offer->price; $brand= $offer->brand; $netweight= $

我有一个下拉列表,用户可以在其中选择不同的netweight。例如200ml、300ml等

      <?php 
      foreach($todays_offers as $offer)
      {
         $productname= $offer->product_name;
         $price= $offer->price;
         $brand= $offer->brand;
         $netweight= $offer->netweight;
         <div><?php echo $productname ?></div>
         <div><?php echo $price ?></div>
         <div><?php echo $brand ?></div>
       sql queries come here....
      <select name='netweight' id='netweight' onchange='ItemSelected(this)'>
        foreach ($query1->result() as $row) 
        {
            $net = $row->packing; 
            ?>
            <option id="<?php echo $row->id;?>" value="<?php echo $net;?>"><?php 
            echo $net;?>                            
           </option>
      </select> 
 <div class="product-button">
 <a class="add_cart" data-pname="<?php echo $productname ?>"  data-netweight="<?php echo $netweight?>"  data-price="<?php echo $price?>">
 add to cart</a></div>
}?>
参考此图
从选择中删除onchange

<?php 
      foreach($todays_offers as $offer)
      {
         $productname= $offer->product_name;
         $price= $offer->price;
         $brand= $offer->brand;
         $netweight= $offer->netweight;
         <div><?php echo $productname ?></div>
         <div><?php echo $price ?></div>
         <div><?php echo $brand ?></div>
       sql queries come here....
      <select name='netweight' id='netweight'>
        foreach ($query1->result() as $row) 
        {
            $net = $row->packing; 
            ?>
            <option id="<?php echo $row->id;?>" value="<?php echo $net;?>"><?php 
            echo $net;?>                            
           </option>
      </select> 
 <div class="product-button">
 <a class="add_cart" data-pname="<?php echo $productname ?>"  data-netweight="<?php echo $netweight?>"  data-price="<?php echo $price?>">
 add to cart</a></div>
}?>
我用过父母

$(this).parent().find('a').attr('data-netweight',optionsValue);

var optionsValue=$netweight选项:selected.val;警报选项值$此.a.attr'data-netweight',optionsValue;如果我警告该值,我将得到该值,但它只会警告第一个产品。你能帮我吗?定义select类并使用类而不是id作为选择器。$document.readyfunction{$.classname.changefunction{var optionsValue=$option:selected,this.val;$this.closest'a'.attr'data-netweight',optionsValue;}; };嘿,如果我向AlertOptions Value发出警报,我就能够获取数据。但价值设定者不起作用
$(document).ready(function(){
     $("#netweight").change(function(){
        var optionsValue = $("#netweight option:selected").val();
        $(this).closest('a').attr('data-netweight',optionsValue);
     });
});
$(this).parent().find('a').attr('data-netweight',optionsValue);