Javascript 如何将handleChange方法固定到输出用户';输入期望的结果?

Javascript 如何将handleChange方法固定到输出用户';输入期望的结果?,javascript,reactjs,Javascript,Reactjs,我有两个字段叫做纬度和经度 <TextBoxField id="location-latitude-control" title="Required: Latitude" name="location.latitude" value={location.latitude} handleChange={this.handleChange} handleBlur={this.handleBlur} />

我有两个字段叫做纬度和经度

<TextBoxField
  id="location-latitude-control"
  title="Required: Latitude"
  name="location.latitude"
  value={location.latitude}
  handleChange={this.handleChange}
  handleBlur={this.handleBlur}
/>

<TextBoxField
  id="location-longitude-control"
  title="Required: longitude"
  name="location.longitude"
  value={location.longitude}
  handleChange={this.handleChange}
  handleBlur={this.handleBlur}
/>
纬度的预期行为:

".12" should convert it to "00.120000"
"-1.1", should allow to input "-01.100000"
"-1.23", should allow to input "-01.230000"
"-30", should allow to input "-30.000000"
"12.25", should allow to input "12.25000"
"7.0", should allow to input "07.000000"
"12", should allow to input "12.000000"
".12" should convert it to "000.120000"
"-1.1", should allow to input "-001.100000"
"-1.23", should allow to input "-001.230000"
"-30", should allow to input "-030.000000"
"12.25", should allow to input "012.25000"
"7.0", should allow to input "007.000000"
"12", should allow to input "012.000000"
经度的预期行为:

".12" should convert it to "00.120000"
"-1.1", should allow to input "-01.100000"
"-1.23", should allow to input "-01.230000"
"-30", should allow to input "-30.000000"
"12.25", should allow to input "12.25000"
"7.0", should allow to input "07.000000"
"12", should allow to input "12.000000"
".12" should convert it to "000.120000"
"-1.1", should allow to input "-001.100000"
"-1.23", should allow to input "-001.230000"
"-30", should allow to input "-030.000000"
"12.25", should allow to input "012.25000"
"7.0", should allow to input "007.000000"
"12", should allow to input "012.000000"

使用
mapNumber
功能格式化数据

第一个参数是要格式化的数字,第二个参数是要添加的前导零的数量,第三个参数是要添加的尾随零的数量

函数映射编号(num、前导、尾随){
var s=num.toFixed(training)+;

而(s.length您需要创建两个函数来处理纬度和经度

handleLatitudeChange=e=>{
const val=e.target.value;
常量valSign=数学符号(val);
val=数学绝对值(val).toFixed(6);
if(Math.abs(val)<10)val=`0${val}`;
如果(valSign==-1)val=`-${val}`;
返回val;
}
HandleLongChange=e=>{
const val=e.target.value;
常量valSign=数学符号(val);
val=数学绝对值(val).toFixed(6);
if(Math.abs(val)<10)val=`00${val}`;
else如果(Math.abs(val)<100)val=`0${val}`;
如果(valSign==-1)val=`-${val}`;
返回val;

}
非常感谢David的回复。你的解决方案很有魅力,除了一个问题。当我为latitude输入
-1.1
时,我看到它转换为
-1.100000
,而不是
-01.100000
。在这种情况下,我如何解决这个问题?谢谢Ankit,你的解决方案也很有效。latitude one看起来不错!我相信为经度设置了另一个类似于您在上面为纬度提供的函数。所有场景看起来都不错,除了我为经度输入
12
时,它将数据转换为
12.000000
,而不是
012.000000
。您知道如何解决这个问题吗?非常感谢您提供了这两种方法!它真的帮助了l我真的很感激。