I have a dropdown list where user can choose the different netweight. for example(200ml, 300ml etc).
<?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>
}?>
my question is when the user choose the dropdownlist i want to set the 'data-netweight' attribute of a cart button to a selected drop down value. i have written the javascript code for onchange event and i am getting the selected value but i don know to set it to a datta-netweight attribute.
javascript
function ItemSelected(dropdown)
{
var product_id = dropdown.options[dropdown.selectedIndex].id;
var value = dropdown.options[dropdown.selectedIndex].value;
var linkitem = $('.product-button').find('a').data('netweight');
}
Remove onchange from select
<?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>
}?>
Use id to get selected options
$(document).ready(function(){
$("#netweight").change(function(){
var optionsValue = $("#netweight option:selected").val();
$(this).closest('a').attr('data-netweight',optionsValue);
});
});
我用过parent()
$(this).parent().find('a').attr('data-netweight',optionsValue);
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.