问题描述
我已经尝试过提出的问题,但是我没有找到正确的答案,我正在尝试将值添加为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>
元素用于让用户输入数字。它们包括用于拒绝非数字条目的内置验证,但是元素的真实值仍然是字符串。