如何解决如何在Firestore网站中将值另存为double?
我已经尝试过提出的问题,但是我没有找到正确的答案,我正在尝试将值添加为double值,但是将数据添加为字符串。 这是我的JavaScript
var Price = 0.0;
const priceValue = document.getElementById("price").value;
Price = priceValue;
database.collection("product").doc(uuidv4()).set({
price: Price,});
这是我的html
<div class="inputfield">
<label>Price</label>
<input id="price" type="number" class="input">
</div>
解决方法
问题与风暴无关,但与您处理输入数据有关,您会看到输入字段的值始终是字符串类型,如下面的代码片段所示
function myFun() {
const priceValue = document.getElementById("price").value;
console.log(priceValue,typeof priceValue)
}
<div class="inputfield">
<label>Price</label>
<input id="price" type="number" class="input" value="0.0">
<button onClick="myFun()">Check type</button>
</div>
因此,要在风暴中将值实际保存为数字,您首先需要手动对其进行解析。
function myFun() {
const priceValue = parseFloat(document.getElementById("price").value);
console.log(priceValue,typeof priceValue)
}
<div class="inputfield">
<label>Price</label>
<input id="price" type="number" class="input" value="0.0">
<button onClick="myFun()">Check type</button>
</div>
数字类型的
<input>
元素用于让用户输入数字。它们包括用于拒绝非数字条目的内置验证,但是元素的真实值仍然是字符串。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。