gpt4 book ai didi

reactjs - 在 reactJS/nextJS 中使用按钮启用禁用输入

转载 作者:行者123 更新时间:2023-12-05 04:28:22 24 4
gpt4 key购买 nike

我有这样的情况。如果按下按钮并且可以编辑其中的文本,我想启用文本输入。问题是如何在react js中实现这个功能?

enter image description here

我的代码:

function App() {
return (
<div className="App">
<label>URL : </label>
<input
placeholder='http://localhost:3000/123456'
disabled
/>
<button type='submit'>Active</button>
</div>
);
}

export default App;

最佳答案

像在这个 example 中一样使用 useState 钩子(Hook)和 onClick 事件。

import React, { useState } from "react";

function App() {
const [active, setActive] = useState(false)

return (
<div className="App">
<label>URL : </label>
<input
placeholder='http://localhost:3000/123456'
disabled={!active}
/>
<button
type='submit'
onClick={() => setActive(!active)}
>
Active
</button>
</div>
);
}

关于reactjs - 在 reactJS/nextJS 中使用按钮启用禁用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72637368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com