【问题标题】:i want to disabled button if textbox null in react如果文本框为空,我想禁用按钮
【发布时间】:2020-09-22 04:48:04
【问题描述】:
return (
    {jobstate.jobs.map((data,i) =>{ 
  <form>
    <input type="text" placeholder="Post a comment" onChange={(e) => jobcmthandler(e,data._id,i) } />
    <button type="button" onClick={postcmt} >Send</button>
  </form>
   })}
)

我使用 map 函数生成动态 HTML,如果文本为空,我想禁用按钮,以及如何在 react js 中单击按钮时获取文本值

【问题讨论】:

  • 这能回答你的问题吗? How to disable button in React.js
  • 他们管理单个 from 的状态值,但我有超过 10 个动态生成的表单,所以我如何管理所有表单的状态
  • @doshismit 你能在这里或 codesanbox 添加更多代码吗?帮助其他开发者给你更准确的答案

标签: javascript reactjs react-native mern


【解决方案1】:

我真的不明白你为什么要这样做,但是你去 (example):

import React, { useState } from "react";

export default function App() {
  return ["Name", "Age"].map((label) => <Form label={label} />);
}

function Form({ label }) {
  const [readValue, writeValue] = useState("");
  return (
    <form>
      <label>{label}</label>
      <input
        type="text"
        placeholder="Post a comment"
        onChange={(e) => writeValue(e.target.value)}
        value={readValue}
      />
      <button
        type="button"
        onClick={() => console.log("Submit")}
        disabled={readValue === ""}
      >
        Send
      </button>
    </form>
  );
}

【讨论】:

    猜你喜欢
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2012-10-16
    • 1970-01-01
    • 2020-06-03
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多