简体   繁体   English

为什么输入值显示的是字符串类型而不是数字类型?

[英]why the input value shows a type of string instead of type of number?

I am trying to get the input value type of number.我正在尝试获取数字的输入值类型。 But the value keep showing me its a type of string.但是该值一直向我显示它是一种字符串。 How do i fix this?我该如何解决?

const [price, setPrice] = useState();

const handlePrice = (event) => {
    console.log(event.target.value);
    setPrice(event.target.value);
};

        <div className="col-md-6">
          <label for="inputPrice" className="form-label">
            Price
          </label>
          <input
            value={price}
            onChange={handlePrice}
            type="number"
            className="form-control"
            id="inputPrice"
          />
        </div>
        <div className="col-12">
const handlePrice = (event) => {
    console.log(event.target.value);
    // setPrice(parseInt(event.target.value));
    // setPrice(event.target.value * 1);
    setPrice(Number(event.target.value));
};

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

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