简体   繁体   English

以数字格式获取字符串价格

[英]Getting the string price in numeric format

I am using opencart 3.x and trying to get the price into a number so I can do calculations with it. 我正在使用opencart 3.x并试图将价格换算成数字,以便我可以进行计算。 I keep getting NaN or a scripting error: 我不断收到NaN或脚本错误:

<h2><span id="getPrice">{{ price }}</span> /Carton</h2>    

Here I set id as getPrice, so I can get the price once the page loads, it comes out to $169.99. 在这里,我将id设置为getPrice,因此页面加载后我就可以获取价格,价格为169.99美元。 This number cannot be utilized as a number..this is the issue I am having. 这个数字不能用作数字。这是我遇到的问题。 I know the { { price } } is PHp I believe, but I think since the number loads before my script, I don't have to worry about it? 我知道{{price}}是PHp,但是我认为既然数字在脚本之前加载,我不必担心吗? Once the page loads, it says $116.99 /per carton, I spanned out the { { price } } so I can turn just that into a number. 页面加载完毕后,它说每箱$ 116.99,我将{{price}}摊开,这样我就可以把它变成一个数字。

Here is my javascript. 这是我的JavaScript。 i tried parsefloat which doesnt work, and toFixed(2) which alawys gets the " toFixed is not a function" error 我尝试了不起作用的parsefloat,并且alawys的toFixed(2)收到“ toFixed不是函数”错误

const getPrice = document.querySelector("[id^='getPrice']").innerHTML;
const getSquareFeetPerBox = document.querySelector("[id^='input-option']").value;
const costPerSqft = getPrice / getSquareFeetPerBox;
const fixxed = parseFloat(getPrice);
document.getElementById("pricePerSqft").innerHTML = ` ( ${costPerSqft}  /per sqft )`;
console.log(fixxed);    

Please help, I have been at this for hours and just need to do a simple calculation with the price. 请帮助,我已经在这里待了几个小时,只需要对价格做一个简单的计算即可。 I'm using console.log just to see if a number is logged but its always $116.99 or script error...never just 116.99 我正在使用console.log只是为了查看是否记录了一个数字,但始终为$ 116.99或脚本错误...从不只是116.99

I thank you in advance 我提前谢谢你

Assuming the following HTML: 假设以下HTML:

<h2><span id="getPrice">$169.99</span> /Carton</h2>  
<input type="text" id='input-option' value="10"></input>
<p id="pricePerSqft"> </p>

With the following JS: 使用以下JS:

const getPrice = document.querySelector("[id^='getPrice']").innerHTML;
const getSquareFeetPerBox = document.querySelector("[id^='input-option']").value;
const fixed = parseFloat(getPrice.slice(1, getPrice.length));
const costPerSqft = fixed / getSquareFeetPerBox;
console.log(costPerSqft)
document.getElementById("pricePerSqft").innerHTML = ` ( ${costPerSqft}  / per sqft );

You should be able to parse the number. 您应该能够解析该数字。 I used String#slice to grab the number but per the comments on your original post there are definitely other ways to grab the number. 我使用String#slice来获取数字,但是根据您对原始帖子的评论,肯定有其他方法可以获取数字。 Using a regex like Mark suggested sounds like a solid option :) 使用像Mark这样的正则表达式建议听起来像是一个可靠的选择:)

CodePen link here if you need it: https://codepen.io/travishaby/pen/jRbqMr 如果需要,可在此处链接CodePen: https ://codepen.io/travishaby/pen/jRbqMr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM