猿问

如何在 firestore web 中将值保存为双精度值?

我已经尝试过提出的问题,但我没有找到正确的答案,我试图将一个值添加为双精度值,但将数据添加为字符串。这是我的 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>


宝慕林4294392
浏览 114回答 1
1回答

白猪掌柜的

问题与风暴无关,而是与您对输入数据的处理有关,您会看到输入字段的值始终是字符串类型,如下面的代码片段所示function myFun() {&nbsp; const priceValue = document.getElementById("price").value;&nbsp; console.log(priceValue, typeof priceValue)}<div class="inputfield">&nbsp; <label>Price</label>&nbsp; <input id="price" type="number" class="input" value="0.0">&nbsp; <button onClick="myFun()">Check type</button></div>因此,为了在 firestorm 中实际将值保存为数字,您首先需要手动解析它。function myFun() {&nbsp; const priceValue = parseFloat(document.getElementById("price").value);&nbsp; console.log(priceValue, typeof priceValue)}<div class="inputfield">&nbsp; <label>Price</label>&nbsp; <input id="price" type="number" class="input" value="0.0">&nbsp; <button onClick="myFun()">Check type</button></div><input>number 类型的元素用于让用户输入数字。它们包括内置验证以拒绝非数字条目,但是元素的真实值仍然是一个字符串。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答