可以在ReactJ中的输入中放置超过3个数字
我在ReactJS代码上有一个大问题。我有一个仅输入数字“ EC”的输入是一个数字。用户可以直接输入他的数据,也可以按按钮 +或 - 添加或删除1000。按钮+/-工作,但我的输入无法工作。
错误:
- 当我在键盘中按DELETE时,2000的“ 000”都会一次删除,
- 我无法将4个数字放在输入中
- 当我删除所有内容时, 在输入中。
我尝试了很多事情,例如:
- 将我的输入传递到type ='number'中,
- 将函数直接放在onChange {}
- put number()而不是parseint()
中,没有任何可行。你能帮助我吗 ?
function Resistance() {
const [EC, setEC]= React.useState(2000)
function handlePressEC() {
const testEC=EC-1000
if(testEC<=0) {
setEC(0)
}
else {
setEC(testEC)
}
}
function handleChangeEC(e) {
const EC = parseInt(e.target.value)
setEC(EC)
console.log(EC)
}
return (
<div>
<div className='flexLigneOF flexLigneV'>
<div className='textOF'>Economie de charge</div>
<div className='flexSaisie'>
<button className='buttonPM' onClick={handlePressEC} >
<div className='TextPM'> - </div>
</button>
<input className='inputResi'value={EC.toLocaleString().toString()} onChange={handleChangeEC} keyboardType='number-pad'/>
<button className='buttonPM' onClick={() => {setEC(parseInt(EC)+1000)}}>
<div className='TextPM'> + </div>
</button>
</div>
</div>
</div>
);
}
export default Resistance;
I've got a big issue on my ReactJS code. I have an input to enter only number "EC" is suppose to be a number. The user can enter his data directly in the put or he can press the button + or - to add or remove 1000. The button +/- works but my input wont work.
Errors :
- when I press delete in my keyboard, the "000" of 2000 are all delete at once
- I can't put more that 4 numbers in the input
- when I delete everything an "NaN" appear and I can't put anthing else in the input.
I tried a lot of things such as:
- pass my input in a type='number'
- put the function directly in the onChange{}
- put Number() instead of parseInt()
Nothing works. Can you help me ?
function Resistance() {
const [EC, setEC]= React.useState(2000)
function handlePressEC() {
const testEC=EC-1000
if(testEC<=0) {
setEC(0)
}
else {
setEC(testEC)
}
}
function handleChangeEC(e) {
const EC = parseInt(e.target.value)
setEC(EC)
console.log(EC)
}
return (
<div>
<div className='flexLigneOF flexLigneV'>
<div className='textOF'>Economie de charge</div>
<div className='flexSaisie'>
<button className='buttonPM' onClick={handlePressEC} >
<div className='TextPM'> - </div>
</button>
<input className='inputResi'value={EC.toLocaleString().toString()} onChange={handleChangeEC} keyboardType='number-pad'/>
<button className='buttonPM' onClick={() => {setEC(parseInt(EC)+1000)}}>
<div className='TextPM'> + </div>
</button>
</div>
</div>
</div>
);
}
export default Resistance;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我删除了您的
tolocalestring()
,这是阻止您输入4个以上数字的原因。然后,我更改了您的Onchange功能。您正在转换为一个字段值的数字。但是您无法将无效的值转换为整数。因此,我有条件地更新了状态:
如果您字段的值长度大于一个(因此您的字段不为空),则设置状态。但是否则,将状态设置为0。
这为您提供了一个工作字段,但是如果您的EC变量等于0,则输入字段的内部为0(我想您不想要)。
我将您的输入的值属性更改为
value = {ec&gt; 0? ec.tostring():“”}
(如果EC&gt; 0,请放置EC,否则将任何内容放在里面)
这是完整的代码:
Okay, I removed your
toLocaleString()
which is the thing that was preventing you from entering more than 4 numbers.Then I changed a bit your onChange function. You were converting to a number the value of your field. But you can't convert a null value to an integer. So I updated the state conditionally :
If the length of the value of you field is greater than One (So your field is not empty) then set the state. But else, set the state to 0.
This gives you a working field but if your EC variable is equal to 0, the input field has a 0 inside (which I guess you don't want).
I changed the value attribute of your input to
value={EC > 0 ? EC.toString() : ""}
(If EC > 0, put EC, else put nothing inside)
Here's the complete code :