为了账号安全,请及时绑定邮箱和手机立即绑定

如果反应中的文本框为空,我想禁用按钮

如果反应中的文本框为空,我想禁用按钮

白衣非少年 2023-03-18 17:48:22
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 并且我想禁用按钮,如果文本为 null for induvial form 以及如何在 react js 中单击按钮时获取文本值
查看完整描述

1 回答

?
梦里花落0921

TA贡献1772条经验 获得超5个赞

我真的不明白你为什么要这样做但是:

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>

  );

}


查看完整回答
反对 回复 2023-03-18
  • 1 回答
  • 0 关注
  • 63 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信