简体   繁体   English

当选中不工作的reactjs时,复选框会扩展另一个复选框

[英]Checkbox expands another checkboxes when checked not working reactjs

I have a mapping function which shows JSON values into checkboxes, I'm tried to add the children of these values as checkboxes under the current checkboxes, and it worked fine, but what I am trying to do now is make the show the 2 children checkbox once the parent checkbox is clicked. 我有一个映射函数,它将JSON值显示为复选框,我试图将这些值的子项添加为当前复选框下的复选框,并且它工作正常,但我现在要做的是让节目成为2个孩子单击父复选框后复选框。

I have Side Collection 1 and under it there are children (side 1 and side 2), what I tried but couldn't do is show (side 1 and side 2 checkboxes) once (Side Collection 1 checkbox) is checked, the checkbox values are passed as props from Checkbox.js to Itemlist.js where the fetch/map happens. 我有Side Collection 1,在它下面有孩子(第1面和第2面),我尝试但不能做的是显示(第1侧和第2侧复选框)一次(Side Collection 1复选框)被选中,复选框值作为道具从Checkbox.js传递到Itemlist.js ,其中fetch / map发生。 Would Appreciate it if anyone can explain or demonstrate how this can be achieved. 如果有人能够解释或证明如何实现这一点,那么会感激。

I have done a the function i want in vanillajs but im not sure how to add it to my react app specially that is within a mapping function, im new to react 我已经在vanillajs中完成了我想要的功能,但我不知道如何将它添加到我的反应应用中,特别是在映射函数中,我是新的反应

Function snippet: https://codepen.io/alaafm/pen/eXGZbO?editors=1010 功能片段: https//codepen.io/alaafm/pen/eXGZbO? edit = 1010

React Live Snippet: https://codesandbox.io/embed/5w8vwwmn5p?fontsize=14 React Live Snippet: https ://codesandbox.io/embed/5w8vwwmn5p fontize = 14

Checkbox.js 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 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 Function which i want to add 我要添加的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);

You can use internal state in Checkboxes.js and conditional rendering to show sub-checkboxes. 您可以在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;

You can take advantage of react component state and for your checkbox.js component. 您可以利用react组件状态和checkbox.js组件。 If state changes, then the component will re-render with the correct checkboxes. 如果状态发生更改,则组件将使用正确的复选框重新呈现。

I would suggest having a state with a boolean to know if the slide collection checkbox is selected or not, and have the onChange function of your parent checkbox input look something like this: 我建议使用一个布尔值的状态来知道是否选中了幻灯片集合复选框,并且让父复选框输入的onChange函数看起来像这样:

<input
    id={this.props.childk + this.props.name}
    name="checkbox"
    type="checkbox"
    onChange={()=> this.setState({parentCheckbox:!this.state.parentCheckbox})}
/>

Then in your method where you map through this.props.options to render the children checkboxes, you can have a conditional statement for just the input tag that will have it render only if this.state.parentCheckbox is true - see the method below. 然后在您通过this.props.options映射以呈现子复选框的方法中,只有当this.state.parentCheckbox为true时,才能为输入标记创建一个条件语句,只有这样才能呈现 - 请参阅下面的方法。

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"
              onChange={()=> this.setState({parentCheckbox:!this.state.parentCheckbox})}
            />
            <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">
                    { this.state.parentCheckbox && (<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>
    );
  }
}

暂无
暂无

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

相关问题 ReactJS:选中另一个复选框时,取消选中先前选中的复选框 - ReactJS: Uncheck previously checked checkbox when another checkbox is checked 如果选中另一个复选框,如何取消选中一组复选框 - How to uncheck a group of checkboxes when another checkbox is checked 当选中一个div中的复选框时,如何取消取消另一个div中的复选框 - How to unckeck checkboxes in another div when checkbox in one div is checked 选中了带有另一个复选框的复选框 - Check checkboxes with another checkbox has been checked 选中其他复选框时禁用复选框 - Disable checkboxes when another is checked 选中复选框时启用多个复选框 - enable multiple checkboxes when a checkbox is checked 如果选中了2个特定的复选框,则显示一个div;如果选中了1个复选框,则显示另一个(如果选中了2个复选框,则不显示) - Show a div if 2 specific checkboxes are checked, show another if 1 checkbox is checked (don't show it if the 2 checkboxes are checked) 确定何时在 ReactJS 中至少选中了一个复选框 - Determine when at least one checkbox is checked in ReactJS 当“ Any”复选框被选中无法使用时,取消选中组中的所有复选框 - Uncheck all checkboxes in a group when “Any” checkbox is checked used to work not working anymore 我正在尝试选中其主题复选框的tbody的所有复选框,但无法正常工作 - I am trying to tick all the checkboxes of tbody when checkbox of its thead is checked but its not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM