无法解析"4".至4 [英] Failed to parse "4." to 4

查看:90
本文介绍了无法解析"4".至4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React-redux并创建一个数字类型字段. event.target.value 返回一个字符串,因为它是数字类型编辑器,它必须将值作为数字返回,因此我将值解析为数字.当我尝试输入 4.5 时,问题就来了.首先,当我输入4时,它将被解析为4(按数字编号).但是,当.输入数字 4.时,解析为 4 ,所以我只是无法输入浮点值.

I am working in React-redux and creating a number type field. The event.target.value returns a string, since it is number type editor it must return value as number so I parse the value to number. The problem comes when I try to enter 4.5 . Firstly, when I type 4 it will get parsed into 4 (sting to number). But when the . entered the number 4. get parse into 4 , so I just failed to enter the floating point values.

let onChangeText =(值)=> { fieldProps.onChange(parseFloat(value)); }

let onChangeText = (value)=> { fieldProps.onChange(parseFloat(value)); }

如果我解析 onBlur 中的值,则可以使用,但是对于状态管理,我必须使用 onChange 进行操作.有什么方法可以将 4.字符串解析为 4.数字.

If I parse the value in onBlur then it will work but for state management I have to work on onChange . Is there any way to parse 4. string to 4. number .

推荐答案

您可以在状态中存储用户的文字输入,然后应用所需的逻辑并将结果分别存储在父元素的状态中?

You could store user's literal input in your state and then apply the desired logic and store the result separately in the state of a parent element?

这是一种潜在的方法:在React的html输入中的美分和美元之间进行转换

Here is a potential approach: translating between cents and dollars in html input in React

这篇关于无法解析"4".至4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆