javascript - 如果文本框为空,我想禁用按钮
问题描述
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,如果文本为空,我想禁用按钮,以及如何在反应 js 中单击按钮时获取文本值
解决方案
我真的不明白你为什么要这样做,但你去(例子):
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>
);
}
推荐阅读
- hive - 如何从 Hive 中的两个表中获取不匹配的记录?
- apexcharts - Apexcharts - 启用缩放图标时条形图被切断
- android - React Native Android 应用程序在 Facebook 登录/取消时崩溃
- javascript - 使视频填充屏幕宽度并删除框架
- java - 如何从 Java 的 JDI 中隐藏变量?
- javascript - Tampermonkey 在 YouTube 上运行加载和注入代码
- laravel-livewire - 我为 livewire / alpine 应用程序搜索用户空闲插件/决策
- php - 试图理解 php mysqli multi_query
- html - 我无法将样式链接到 Sublime Text 中的 html
- javascript - 即使我有一个数组,map 也不是一个函数