【发布时间】:2019-08-04 04:35:51
【问题描述】:
我有一个将 JSON 值显示到复选框中的映射函数,我尝试将这些值的子项添加为当前复选框下的复选框,并且效果很好,但我现在要做的是制作节目单击父复选框后的 2 个子复选框。
我有 Side Collection 1,在它下面有孩子(side Collection 1 和 side 2),我尝试但不能做的是显示(side Collection 1 复选框)一次(Side Collection 1 复选框),复选框值作为道具从 Checkbox.js 传递到 fetch/map 发生的 Itemlist.js。如果有人能解释或演示如何实现这一点,将不胜感激。
我已经在 vanillajs 中完成了我想要的功能,但我不确定如何将它添加到我的反应应用程序中,特别是在映射功能中,我是新的反应
函数sn-p: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);
【问题讨论】:
标签: javascript reactjs checkbox ecmascript-6 radio-button