我有一个下拉列表,用户可以在其中选择不同的净重。例如(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>
}?>
我的问题是当用户选择我要设置的下拉列表时 &#39;数据净重&#39;购物车按钮的属性为选定的下拉值。我已经为onchange事件编写了javascript代码,我得到了所选的值,但我不知道将它设置为datta-netweight属性。
的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');
}
答案 0 :(得分:1)
从选择
中删除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>
}?>
使用ID获取所选选项
$(document).ready(function(){
$("#netweight").change(function(){
var optionsValue = $("#netweight option:selected").val();
$(this).closest('a').attr('data-netweight',optionsValue);
});
});
答案 1 :(得分:0)
我使用了parent()
$(this).parent().find('a').attr('data-netweight',optionsValue);