可以在ReactJ中的输入中放置超过3个数字

发布于 2025-02-10 04:47:22 字数 1507 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2025-02-17 04:47:22

好的,我删除了您的tolocalestring(),这是阻止您输入4个以上数字的原因。

然后,我更改了您的Onchange功能。您正在转换为一个字段值的数字。但是您无法将无效的值转换为整数。因此,我有条件地更新了状态:

  function handleChangeEC(e) {
    if (e.target.value.length > 0) {
      let newEc = parseInt(e.target.value);
      setEC(newEc);
    } else {
      setEC(0);
    }
  }

如果您字段的值长度大于一个(因此您的字段不为空),则设置状态。但是否则,将状态设置为0。

这为您提供了一个工作字段,但是如果您的EC变量等于0,则输入字段的内部为0(我想您不想要)。

我将您的输入的值属性更改为value = {ec&gt; 0? ec.tostring():“”}
(如果EC&gt; 0,请放置EC,否则将任何内容放在里面)

这是完整的代码:

import { useState } from "react";

function Resistance() {
  const [EC, setEC] = useState(2000);

  function handlePressEC() {
    const testEC = EC - 1000;
    if (testEC <= 0) {
      setEC(0);
    } else {
      setEC(testEC);
    }
  }

  function handleChangeEC(e) {
    if (e.target.value.length > 0) {
      let newEc = parseInt(e.target.value);
      setEC(newEc);
    } else {
      setEC(0);
    }
  }

  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 > 0 ? EC.toString() : ""}
            onChange={handleChangeEC}
          />
          <button
            className="buttonPM"
            onClick={() => {
              setEC(parseInt(EC) + 1000);
            }}
          >
            <div className="TextPM"> + </div>
          </button>
        </div>
      </div>
      <div>EC : {EC}</div>
    </div>
  );
}

export default Resistance;

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 :

  function handleChangeEC(e) {
    if (e.target.value.length > 0) {
      let newEc = parseInt(e.target.value);
      setEC(newEc);
    } else {
      setEC(0);
    }
  }

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 :

import { useState } from "react";

function Resistance() {
  const [EC, setEC] = useState(2000);

  function handlePressEC() {
    const testEC = EC - 1000;
    if (testEC <= 0) {
      setEC(0);
    } else {
      setEC(testEC);
    }
  }

  function handleChangeEC(e) {
    if (e.target.value.length > 0) {
      let newEc = parseInt(e.target.value);
      setEC(newEc);
    } else {
      setEC(0);
    }
  }

  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 > 0 ? EC.toString() : ""}
            onChange={handleChangeEC}
          />
          <button
            className="buttonPM"
            onClick={() => {
              setEC(parseInt(EC) + 1000);
            }}
          >
            <div className="TextPM"> + </div>
          </button>
        </div>
      </div>
      <div>EC : {EC}</div>
    </div>
  );
}

export default Resistance;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文