在reactjs中提交后,如何禁用ANTD表格提交按钮?

发布于 2025-02-07 19:25:35 字数 1023 浏览 0 评论 0 原文

我有这样的ANTD表格: -

<Form {...layout} form={form} name={formTitle(window.location.pathname)} onFinish={submitForm}>
    <Form.Item name="name" label="Name" rules={NAME_VALIDATION}>
        <Input value={name} onChange={({ target: { value, } }) => setName(value)}/>
    </Form.Item>
    <Form.Item {...tailLayout}>
        <Button type="primary" htmlType="submit">{formCardTitle(window.location.pathname)}</Button> &nbsp;
        <Button htmlType="button" onClick={onReset}>
        Reset
        </Button>
    </Form.Item>
</Form>

我想禁用表单上的提交按钮。类似的内容: -

const submitForm = async () => {
    .................
    .................
    // disable submit button
}

我知道如何在表单中禁用按钮,类似的内容: -

<Button type="primary" htmlType="submit" disabled={!(name)}>
{formCardTitle(window.location.pathname)}
</Button>

但是我只想在表单提交后仅在submitform()函数内禁用按钮。 我该怎么做?

I have an antd form like this:-

<Form {...layout} form={form} name={formTitle(window.location.pathname)} onFinish={submitForm}>
    <Form.Item name="name" label="Name" rules={NAME_VALIDATION}>
        <Input value={name} onChange={({ target: { value, } }) => setName(value)}/>
    </Form.Item>
    <Form.Item {...tailLayout}>
        <Button type="primary" htmlType="submit">{formCardTitle(window.location.pathname)}</Button>  
        <Button htmlType="button" onClick={onReset}>
        Reset
        </Button>
    </Form.Item>
</Form>

I want to disable the submit button on form submit. Something like this:-

const submitForm = async () => {
    .................
    .................
    // disable submit button
}

I know how to disable the button within the form, something like this:-

<Button type="primary" htmlType="submit" disabled={!(name)}>
{formCardTitle(window.location.pathname)}
</Button>

But I want to disable the button only after form submit, inside the submitForm() function.
How can I do that?

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

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

发布评论

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

评论(1

末が日狂欢 2025-02-14 19:25:35

您可以将其用于布尔

这里一个示例

import { useState } from "react";

const Form = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [btnDisable, setBtnDisable] = useState(false);


const emailHandler = (e) => setEmail(e.target.value);
const nameHandler = (e) => setName(e.target.value);

const submitHandler = (e) => {
e.preventDefault();
if (!name && !email.includes("@")) {
  return;
}
const data = { name, email };
// send data to backend;
// after the sent data successfully
setBtnDisable(true);

//clear the input fields
setName("");
setEmail("");
};

return (
<form onSubmit={submitHandler}>
  <input type="text" value={name} onChange={nameHandler} />
  <input type="email" value={email} onChange={emailHandler} />
  <button type="submit" disabled={btnDisable}>
    submit
  </button>
</form>
 );
};
export default Form;

You can use for that the Boolean

Here is the actual implementation in codesandbox

Here the one Example

import { useState } from "react";

const Form = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [btnDisable, setBtnDisable] = useState(false);


const emailHandler = (e) => setEmail(e.target.value);
const nameHandler = (e) => setName(e.target.value);

const submitHandler = (e) => {
e.preventDefault();
if (!name && !email.includes("@")) {
  return;
}
const data = { name, email };
// send data to backend;
// after the sent data successfully
setBtnDisable(true);

//clear the input fields
setName("");
setEmail("");
};

return (
<form onSubmit={submitHandler}>
  <input type="text" value={name} onChange={nameHandler} />
  <input type="email" value={email} onChange={emailHandler} />
  <button type="submit" disabled={btnDisable}>
    submit
  </button>
</form>
 );
};
export default Form;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文