简体   繁体   English

复选框中的 React JS onChange 事件无法正常工作

[英]React JS onChange event in checkbox is not working properly

codesandbox.io/s/beautiful-sid-dy97k?file=/src/App.js demo url for checkbox codeandbox.io/s/beautiful-sid-dy97k?file=/src/App.js 演示 url 用于复选框

  const handleOnChange = async (position) => {
    const updatedCheckedState = Filter.contract.map((item, index) =>
      index === position ? !item : item
    );
    await setFilter((e) => ({ ...e, contract: updatedCheckedState }));
  };


// this is checkbox code
                        {[
                          "Bank",
                          "Agency",
                          "Permanent",
                          "Ad Hoc",
                          "Temporary",
                          "Part Time",
                          "Contractual"
                        ].map((item, i) => {
                          return (
                            <Form.Group controlId="formBasicCheckbox">
                              <Form.Check
                                onChange={() => handleOnChange(i)}
                                type="checkbox"
                                label={item}
                              />
                            </Form.Group>
                          );
                        })}

this is my checkbox logic and when i click on any of label then first item checked.这是我的复选框逻辑,当我单击 label 中的任何一个时,然后检查第一项。 I don't know how to slove it我不知道如何爱它

actuall behavior should be that If user select on label then it should also checked.实际行为应该是如果用户 select 在 label 上,那么它也应该检查。

so problem was in my <form.check> tag here we need to give ID in that tag like所以问题出在我的 <form.check> 标记中,我们需要在该标记中提供 ID,例如

<Form.Check
 id={i.toString()} // unique id should be there
 onChange={() => handleOnChange(i)}
 type="checkbox"
 label={item}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM