【问题标题】:How to get each item's value in React.js如何在 React.js 中获取每个项目的值
【发布时间】:2021-05-20 13:49:20
【问题描述】:
const [state, setState] = useState("");
<input onChange={e=>setState(e.target.value)}

我知道我可以使用 onChange={} 来获取每个值,但我想获取 (衬衫 x 裤子 x 鞋子)当按钮 onClick={} 事件发生时

function Somethingie() {
   return (
      <div>
         Shirts: <input className="one" type="number"></input>
         Pants: <input className="two" type="number"></input>
         Shoes: <input className="three" type="number"></input>
         <br /> <br />
         <button
            onClick={() => {
               console.log(
                  document.querySelector(".one").value *
                     document.querySelector(".two").value *
                     document.querySelector(".three").value
               );
            }}
         >
            Calculate!
         </button>
         <br /> <br />
         Amount of Possible Outfits: <h1 className="result"></h1>
      </div>
   );
}
ReactDOM.render(
   <Somethingie />, 
   document.getElementById("root")
);

还是创建一个异步函数?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    我会为每个输入创建一个状态,将各个输入的值设置为这个状态,为每个输入设置onChange的状态,最后,在按钮的onClick中将三个状态组合起来。

    示例输入:

    const [state1, setState1] = useState("")
    ...
    const change1 = (e) => {
       setState1(e.target.value);
    }
    ...
    <input className="one" type="number" value={state1} onChange={change1}/>
    

    点击按钮

    const handleClick = () => {
       result = state1 * state2 * state3;
    }
    
    

    【讨论】:

    • 我是否需要为此了解 promises 或 async 函数?
    • 不,你没有。只是简单的反应 JavaScript
    • 如何获取我的 onClick 函数 onClick{(var0, var1, var2)=>result}}的每个状态值
    • 状态就像变量一样可用。您不需要将它们列为参数。查看我的最新编辑
    【解决方案2】:
        function Somethingie() {
            return (
                   <div>
                  Shirts: <input className="one" type="number"></input>
                  Pants: <input className="two" type="number"></input>
                  Shoes: <input className="three" type="number"></input>
                  <br /> <br />
                  <button
                     onClick={() => {
                          let x = document.getElementsByClassName('result');
                          x[0].innerHTML =
                          document.querySelector('.one').value *
                          document.querySelector('.two').value *
                          document.querySelector('.three').value;
                             }}
                >
                     Calculate!
                </button>
                <br /> <br />
               Amount of Possible Outfits: <h1 className="result"></h1>
               </div>
             );}
    
       ReactDOM.render(
       <Somethingie />, 
       document.getElementById("root")
       );
    

    如上代码sn-p所示,可以直接放置计算值。

    【讨论】:

    • 为什么是 x[0]? x 不是数组,对吧?除了一切都清楚。
    • x[0] 因为在上面的 sn-p 中,x 是具有“结果”类的元素数组,在我们的例子中我们只使用了一次结果类,这就是 x[0] 的原因。 (x 的输出是 HTMLCollection(2) [h1.result] 0: h1.result length: 1 )如果我们有多个使用类“result”的元素,那么我们需要指定它们的索引。
    【解决方案3】:

    您需要像这样将每个数据保存在自己的 useState 中:

    import { useState } from "react";
    
    const Combinations = () => {
      const [shirts, setShirts] = useState(0);
      const [pants, setPants] = useState(0);
      const [shoes, setShoes] = useState(0);
      const [total, setTotal] = useState(0);
    
      const onChange = (e) => {
        console.log(e);
        console.log(typeof e.target.value);
        console.log(Number(e.target.value));
    
        if (e.target.name === "shirts") setShirts(Number(e.target.value));
        if (e.target.name === "pants") setPants(Number(e.target.value));
        if (e.target.name === "shoes") setShoes(Number(e.target.value));
      };
    
      const calculate = () => {
        let all = null;
    
        if (shirts !== 0) {
          if (all === null) {
            all = shirts;
          } else {
            all *= shirts;
          }
        }
        if (pants !== 0) {
          if (all === null) {
            all = pants;
          } else {
            all *= pants;
          }
        }
        if (shoes !== 0) {
          if (all === null) {
            all = shoes;
          } else {
            all *= shoes;
          }
        }
    
        setTotal(all);
      };
    
      return (
        <div>
          <label htmlFor="shirts">Shirts: </label>
          <input
            type="number"
            id="shirts"
            name="shirts"
            value={shirts}
            onChange={onChange}
          />
          <br />
          <label htmlFor="pants">Pants: </label>
          <input
            type="number"
            id="pants"
            name="pants"
            value={pants}
            onChange={onChange}
          />
          <br />
          <label htmlFor="shoes">Shoes: </label>
          <input
            type="number"
            id="shoes"
            name="shoes"
            value={shoes}
            onChange={onChange}
          />
          <br />
          <button onClick={calculate}>Calculate!</button>
          <br />
          <p>Amount of Possible Outfits: {total}</p>
        </div>
      );
    }
    

    【讨论】:

    • 是否在 onChange(e) 监听该组件中的任何事件?
    • 这是我们给 react 的事件监听函数,当用户改变输入值时调用。
    • 我错过了 一切都很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多