onChange事件未触发在React JS中

发布于 2025-02-11 06:46:01 字数 4869 浏览 3 评论 0原文

export default function ActionRolesPage(props) {
      const [authorities, setAuthorities] = useState([]);
      const [name, setName] = useState("");
      let list = [];
      useEffect(() => {
        getAuthorities();
      }, []);
      const getAuthorities = () => {
        doGetAllAuthorities()
          .then((res) => {
            getValidatedData(res.data, "array").map((data) => (
              list.push({ authority: data})
            ))
            setAuthorities(list);
          }).catch((e) => {
            console.log(e);
          })
      }
      const handleChange = (e) => {
        console.log(e);
        const { name, checked } = e.target
        console.log(name,checked);
        let tempUser = authorities.map((user) => (
          user.authority === name ? { ...user, isChecked: checked } : user
        ));
        setAuthorities(tempUser);
      }
    
      if(authorities.length){
        console.log(authorities);
      }
    
      
      return (
        <React.Fragment>
          <Suspense fallback={<div>Loading....</div>}>
            <div className="page-content">
              <MetaTags>
                <title>Add Role | IG One</title>
              </MetaTags>
              <Container fluid>
                <Breadcrumb
                  title="Add Role"
                  breadcrumbItems={[{ title: "Settings" }, { title: "Roles" }, { title: "Add" }]}
                />
                <Form onSubmit={handleSubmit}>
                  <Card>
                    <CardBody>
                      <Row className="mb-3">
                        <label
                          htmlFor="example-text-input"
                          className="col-md-2 col-form-label"
                        >
                          Name
                        </label>
                        <div className="col-md-8 mx-0">
                          <Input
                            className="form-control"
                            type="text"
                            name="name"
                            required
                            value={name}
                            onChange={(e) => setName(e.target.value)}
                            placeholder="Name Of User "
                          />
                        </div>
                      </Row>
                      <br></br>
                      <br></br>
                      <Row className="mb-3">
                        <CardTitle>
                          Authorities
                        </CardTitle>
                        <div className="col-md-2">
    
                          {
                           
                              authorities.map((data,index) => (
                                <>
                                  <div key={index} style={{ display: "flex" }}>
                                    <div className='col-md-10 mx-0 mt-2'>
                                      <Input type={"checkbox"}
                                        checked={data?.isChecked || false}
                                        name={data.authority}
                                        onChange={(e) => console.log(e)}
                                        className="form-control"
                                        style={{ cursor: "pointer", paddingLeft: "1rem" }}
                                      /></div>
                                    <div>
                                      <label style={{ cursor: "pointer" }} htmlFor={data.authority} className="col-md-50 col-form-label"> {data.authority}</label>
                                    </div>
                                  </div>
                                </>
                              )) 
                          }
                        </div>
                      </Row>
                      <Row className="d-flex justify-content-center mt-4">
                        <Button color="dark" type='submit' className="btn-xs" style={{ width: "40%", cursor: "pointer" }}
                        >
                          Add Role
                        </Button>
                      </Row>
                    </CardBody>
                  </Card>
                </Form>
              </Container>
            </div>
          </Suspense>
        </React.Fragment>
      )
    } 

这是整个代码。我想处理多个复选框,但没有触发Onchange事件。触发OnChange时,它调用了一个函数处理,但是在我的情况下,控制台没有出现错误,并且在控制台上没有显示任何事件,请解决我的疑问。

我还需要更新从后端获得响应的表格,请检查授权名称阵列如何在复选框中处理检查状态。

export default function ActionRolesPage(props) {
      const [authorities, setAuthorities] = useState([]);
      const [name, setName] = useState("");
      let list = [];
      useEffect(() => {
        getAuthorities();
      }, []);
      const getAuthorities = () => {
        doGetAllAuthorities()
          .then((res) => {
            getValidatedData(res.data, "array").map((data) => (
              list.push({ authority: data})
            ))
            setAuthorities(list);
          }).catch((e) => {
            console.log(e);
          })
      }
      const handleChange = (e) => {
        console.log(e);
        const { name, checked } = e.target
        console.log(name,checked);
        let tempUser = authorities.map((user) => (
          user.authority === name ? { ...user, isChecked: checked } : user
        ));
        setAuthorities(tempUser);
      }
    
      if(authorities.length){
        console.log(authorities);
      }
    
      
      return (
        <React.Fragment>
          <Suspense fallback={<div>Loading....</div>}>
            <div className="page-content">
              <MetaTags>
                <title>Add Role | IG One</title>
              </MetaTags>
              <Container fluid>
                <Breadcrumb
                  title="Add Role"
                  breadcrumbItems={[{ title: "Settings" }, { title: "Roles" }, { title: "Add" }]}
                />
                <Form onSubmit={handleSubmit}>
                  <Card>
                    <CardBody>
                      <Row className="mb-3">
                        <label
                          htmlFor="example-text-input"
                          className="col-md-2 col-form-label"
                        >
                          Name
                        </label>
                        <div className="col-md-8 mx-0">
                          <Input
                            className="form-control"
                            type="text"
                            name="name"
                            required
                            value={name}
                            onChange={(e) => setName(e.target.value)}
                            placeholder="Name Of User "
                          />
                        </div>
                      </Row>
                      <br></br>
                      <br></br>
                      <Row className="mb-3">
                        <CardTitle>
                          Authorities
                        </CardTitle>
                        <div className="col-md-2">
    
                          {
                           
                              authorities.map((data,index) => (
                                <>
                                  <div key={index} style={{ display: "flex" }}>
                                    <div className='col-md-10 mx-0 mt-2'>
                                      <Input type={"checkbox"}
                                        checked={data?.isChecked || false}
                                        name={data.authority}
                                        onChange={(e) => console.log(e)}
                                        className="form-control"
                                        style={{ cursor: "pointer", paddingLeft: "1rem" }}
                                      /></div>
                                    <div>
                                      <label style={{ cursor: "pointer" }} htmlFor={data.authority} className="col-md-50 col-form-label"> {data.authority}</label>
                                    </div>
                                  </div>
                                </>
                              )) 
                          }
                        </div>
                      </Row>
                      <Row className="d-flex justify-content-center mt-4">
                        <Button color="dark" type='submit' className="btn-xs" style={{ width: "40%", cursor: "pointer" }}
                        >
                          Add Role
                        </Button>
                      </Row>
                    </CardBody>
                  </Card>
                </Form>
              </Container>
            </div>
          </Suspense>
        </React.Fragment>
      )
    } 

Here is the whole code. I want to handle multiple checkboxes but onChange Event not triggered. There is a function handleChange it calls when onChange triggered but in my case there is no error seen in console as well as not display any event at console please resolve my doubt.

I also need to update the form getting respose from backend is checked authority name array How to handle checked state in checkbox.

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

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

发布评论

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

评论(1

绝對不後悔。 2025-02-18 06:46:01

如果您已经为 Input 创建了一个自定义组件,请将OnChange处理程序作为Prop传递,并在组件中以&lt; Input&gt;字段的方式打电话。或者,如果您使用了任何第三方LIB,则只需将回调作为道具。否则,请尝试以下方法:&lt; input&gt;,而不是&lt; input&gt;

<input type={"checkbox"}
       checked={data?.isChecked || false}
       name={data.authority}
       onChange={(e) => console.log(e)}
       className="form-control"
       style={{ cursor: "pointer", paddingLeft: "1rem" }}
     />  

        

If you have created a custom component for input, pass your onChange handler as prop and call inside the component as onChage event of <input> field. Or if you used any third-party lib, then you just need to pass a callback as prop. Otherwise Try this: <input> instead of <Input>

<input type={"checkbox"}
       checked={data?.isChecked || false}
       name={data.authority}
       onChange={(e) => console.log(e)}
       className="form-control"
       style={{ cursor: "pointer", paddingLeft: "1rem" }}
     />  

        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文