gpt4 book ai didi

javascript - 获取下拉菜单的 href 属性 onchange 的值

转载 作者:行者123 更新时间:2023-12-03 03:05:44 25 4
gpt4 key购买 nike

我有一个下拉列表,用户可以在其中选择不同的净重。例如(200毫升、300毫升等)。

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

我的问题是当用户选择下拉列表时我想设置将购物车按钮的“data-netweight”属性设置为选定的下拉值。我已经为 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');

}

引用这张图片 enter image description here

最佳答案

从选择中删除 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);
});
});

关于javascript - 获取下拉菜单的 href 属性 onchange 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47176684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com