gpt4 book ai didi

javascript - 复选框在选中时扩展另一个复选框不起作用reactjs

转载 作者:行者123 更新时间:2023-11-29 15:59:18 26 4
gpt4 key购买 nike

我有一个将 JSON 值显示到复选框中的映射函数,我尝试将这些值的子项作为复选框添加到当前复选框下,并且它工作正常,但我现在要做的是制作节目单击父复选框后的 2 个子复选框。

我有 Side Collection 1 并且在它下面有 child (side 1 和 side 2),我尝试但不能做的是显示(side 1 和 side 2 复选框)一次(Side Collection 1 复选框)被选中,复选框值作为 props 从 Checkbox.js 传递到 Itemlist.js,在其中进行获取/映射。如果有人能解释或演示如何实现这一点,我们将不胜感激。

我已经在 vanillajs 中完成了我想要的功能,但我不确定如何将它添加到我的 react 应用程序中,特别是在映射函数中,我是 react 新手

函数片段:https://codepen.io/alaafm/pen/eXGZbO?editors=1010

React 实时片段:https://codesandbox.io/embed/5w8vwwmn5p?fontsize=14

Checkbox.js

 class Checkboxes extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<form className="form">
<div>
<h2>{this.props.title}</h2>
<div className="inputGroup">
<input
id={this.props.childk + this.props.name}
name="checkbox"
type="checkbox"
/>
<label htmlFor={this.props.childk + this.props.name}>
{this.props.name}{" "}
</label>
</div>{" "}
{this.props.options &&
this.props.options.map(item => {
return (
<div className="inputGroup2">
{" "}
<div className="inputGroup">
<input
id={this.props.childk + (item.name || item.description)}
name="checkbox"
type="checkbox"
/>
<label
htmlFor={
this.props.childk + (item.name || item.description)
}
>
{item.name || item.description}{" "}
{/** <img src={this.props.img} alt="" /> <span className="pricemod">{props.childprice} SAR</span>
*/}
</label>
</div>
</div>
);
})}
</div>
</form>
);
}
}

export default Checkboxes;

Itemlist.js

...
<div>
{selectedMod &&
selectedMod.children &&
selectedMod.children.map((item, index) => (
<Checkboxes
key={index}
name={item.name || item.description}
myKey={index}
options={item.children}
childk={item.id}
limit={item.max}
/>
))}
</div>
...

我要添加的JS函数

  const myCheck2 = document.getElementById("check2");
const dib = document.getElementById("dib");
function change() {
if (myCheck2.checked) {
dib.style.display = "block";
dib2.style.display = "block";

}
else{
dib.style.display = "none";
dib2.style.display = "none";
}
}
function func2() {
if (this.checked) {
myCheck2.checked = false;
dib.style.display = "none";
}
else {
dib.style.display = "block";
myCheck2.checked = true;
}
}

myCheck2.addEventListener('click', change);

最佳答案

您可以使用 Checkboxes.js 中的内部状态和条件渲染来显示子复选框。

import React from "react";
import "./Checkbox.css";

class Checkboxes extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: false
}
}

render() {

const input2Checkboxes = this.props.options &&
this.props.options.map(item => {
return (
<div className="inputGroup2">
{" "}
<div className="inputGroup">
<input
id={this.props.childk + (item.name || item.description)}
name="checkbox"
type="checkbox"
/>
<label
htmlFor={
this.props.childk + (item.name || item.description)
}
>
{item.name || item.description}{" "}
{/** <img src={this.props.img} alt="" /> <span className="pricemod">{props.childprice} SAR</span>
*/}
</label>
</div>
</div>
);
})

return (
<form className="form">
<div>
<h2>{this.props.title}</h2>
<div className="inputGroup">
<input
id={this.props.childk + this.props.name}
name="checkbox"
type="checkbox"
checked={this.state.checked}
onChange={() => {
this.setState({checked: !this.state.checked})
}}
/>
<label htmlFor={this.props.childk + this.props.name}>
{this.props.name}{" "}
</label>
</div>{" "}
{this.state.checked ? input2Checkboxes : undefined }


</div>
</form>
);
}
}

export default Checkboxes;

关于javascript - 复选框在选中时扩展另一个复选框不起作用reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com