从一个元素 JS 中获取产品价格
Posted
技术标签:
【中文标题】从一个元素 JS 中获取产品价格【英文标题】:Get the product price from a element JS 【发布时间】:2021-10-22 16:02:54 【问题描述】:我有一个脚本可以进行一些计算,但我不知道 如何从元素中获取价格检查下面的代码
/* THE PRICE DOESNT SHOW ON THIS SNIPPET BECAUSE REQUIRES TO BE IN SAME DIRECTORY WITH FILE */
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
</div>
<div class="row">
<div class="col-3">
<label for="qty_product">qty</label>
<input type="number" value="0.01" class="qty" id="qty_product"/>
</div>
<div class="col-2">
<label for="price_product">price</label>
<input type="number" readonly value="0" id="price_product"/>
</div>
</div>
<div class="row my-4">
<div class="col-9 text-right total">
Total
</div>
<div class="col-2 total-val">
<span id="total_value">0</span
</div>
</div>
</div>
<script>
function calculateTotal()
let unit_price=
<!-- the price should be always * 100 -->
product: 304.36 * 100
;
let item_price=
item_price.product = ($("#qty_product").val() * unit_price.product )
$("#price_product").val(item_price.product);
let total = item_price.product;
$("#total_value").text(total);
$(function()
$(".qty").on("change keyup",calculateTotal)
)
</script>
<!-- I WANT THE PRICE TO COME FROM THIS ELEMENT NOT FROM ONE IN THE SCRIPT* -->
<stock-market-widget type="inline" template="generic" assets="MSFT" markup="price" api="yf"></stock-market-widget>
<!-- I WANT THE PRICE TO COME FROM THIS ELEMENT -->
<script>
var premiumStockMarketWidgets =
pluginUrl: '../',
assetsLogoImages: JSON.parse('<?php print json_encode($assetsLogoImages)?>')
</script>
<script src="../assets/dist/app.js"></script>
</body>
</html>
我希望 pcice 来自 stock-market-widget, 总是去* 100,我没有尝试任何方式,因为我是这方面的初学者 javascript之类的!!如果有人提供帮助,我将不胜感激!!! 谢谢和问候!
【问题讨论】:
【参考方案1】:在 value 中添加更多属性
<stock-market-widget type="inline" template="generic" assets="MSFT" value="price" markup="price" api="yf"></stock-market-widget>
<script>
var stockprice =$('stock-market-widget').val();
//now put variable stockprice where you require
</script>
【讨论】:
以上是关于从一个元素 JS 中获取产品价格的主要内容,如果未能解决你的问题,请参考以下文章