React 编辑具有值的输入元素

发布于 2025-01-14 02:23:16 字数 178 浏览 4 评论 0原文

我有这个输入:

  <input
    type="text"
    autoFocus
    value={post.body}
   />

我希望用户能够编辑该值,我尝试设置 contentEditable 但它不起作用

i have this input:

  <input
    type="text"
    autoFocus
    value={post.body}
   />

i want the user to be able to edit the value, i tried to set contentEditable but it doesn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2025-01-21 02:23:16
<input type="text" autoFocus defaultValue={post.body}/>

在您的代码中,您无法编辑值。
因为你设置了value,所以设置defaultValue。

<input type="text" autoFocus defaultValue={post.body}/>

In your code, you can not edit value.
Because you set value, so set defaultValue.

浅唱ヾ落雨殇 2025-01-21 02:23:16

您必须在此输入上设置事件侦听器以捕获正在发生的变化。

您的数据应该是定义的状态变量,因为

const [value, setValue] = useState("");

在您的情况下,默认值可以是 post.body

const [value, setValue] = useState(post.body);

,然后在输入组件中将所有内容连接在一起

 <input
    type="text"
    value={value}
    onChange={(event) => setValue(event.target.value)}
   />

You have to set event listener on this input to capture what is changing.

Your data should be state variable defined as

const [value, setValue] = useState("");

in your case the default value can be post.body

const [value, setValue] = useState(post.body);

and then in the input component connect all together

 <input
    type="text"
    value={value}
    onChange={(event) => setValue(event.target.value)}
   />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文