简体   繁体   English

如何使 Grid 组件可单击以选中/取消选中复选框?

[英]How do I make a Grid component clickable to check/uncheck checkbox?

I have a Grid component as a row among other grid rows within the grid container.我有一个 Grid 组件作为网格容器内其他网格行中的一行。 The grid row contains a description and a checkbox.网格行包含描述和复选框。 How do i make it so I can click anywhere on the row to be able to check/uncheck the checkbox?我如何做到这一点,以便我可以单击行上的任意位置以选中/取消选中复选框?

<label>
              <Grid container xs={12} direction= "row" onClick={handleChange}>
                <Grid item xs={6} md={8}>
                    <h4>Reverse The Online Gambling Ban</h4>
                    <p>The MPA comes with three different sizes of foam and silicone tips and carrying pouch.</p>
                </Grid>
                <Grid item xs={6} md={4} style={{textAlign: 'center', }}>
                <Checkbox 
                style={{marginTop: "10%"}} 
                type="checkbox" 
                name="newsletter-1" 
                disabled={formData.optOut == true} 
                onChange={handleChange}/>
                </Grid>
              </Grid>
 </label>

I am not sure why you want to use Grid But If you want to activate checkbox with by clicking on any text then you can use FormControlLabel我不确定你为什么要使用 Grid 但是如果你想通过单击任何文本来激活复选框,那么你可以使用 FormControlLabel

<FormControlLabel
        control={<Checkbox checked={gilad} onChange={handleChange} name="gilad" />}
        label="Gilad Gray"
      />

Checkbox with FormControlLabel 带有 FormControlLabel 的复选框

You can update the Checkbox component from uncontrolled to controlled.您可以将 Checkbox 组件从不受控制更新为受控制。

First, create a state and call checkboxState as Boolean .首先,创建一个 state 并调用checkboxState作为Boolean
Now, inside your 'handleChange` function toggle the state.现在,在您的“handleChange”中 function 切换 state。

Second, add checked={checkboxState} as a prop to the Checkbox component.其次,将checked={checkboxState}作为属性添加到 Checkbox 组件中。
eg例如

<Checkbox checked={checkboxState} />

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

相关问题 如何使用不可变的js检查和取消选中输入复选框 - how do i check and uncheck input checkbox using immutable js 如何以编程方式选中或取消选中 Angular 8 的复选框? - How do I programmatically check or uncheck my checkbox with Angular 8? 如何取消选中复选框? - How do I uncheck a checkbox? AG-GRID-ANGULAR - 如果选中/取消选中单元格渲染器中的所有复选框,如何选中/取消选中标题组件中的复选框? - AG-GRID-ANGULAR - How to check/uncheck checkbox in header component if all checkboxes in the cell renderers are checked/unchecked? 如何在本机表组件中选中/取消选中单个复选框 - How to check/uncheck individual checkbox in react-native Table component 在Anuglar 1.5.8中,如何使网站的选中/取消选中复选框功能通用? - In Anuglar 1.5.8 How to make check/uncheck checkbox functionality common for a website? 如何选中/取消选中键盘上的复选框 - how to check/uncheck a checkbox on keyup 如何动态选中和取消选中复选框 - how to check and uncheck the checkbox dynamically 如何无限地选中和取消选中复选框? - How to check and uncheck a checkbox infinitely? 使用 vuejs 选中/取消选中子组件中的复选框 - Check / Uncheck a Checkbox in a child component with vuejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM