【问题标题】:Checkbox expands another checkboxes when checked not working reactjs复选框在选中时扩展另一个复选框不起作用reactjs
【发布时间】: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


    【解决方案1】:

    您可以利用 react 组件状态和 checkbox.js 组件。如果状态发生变化,那么组件将使用正确的复选框重新渲染。

    我建议有一个布尔值的状态来知道是否选择了幻灯片集合复选框,并让你的父复选框输入的 onChange 函数看起来像这样:

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

    然后在通过 this.props.options 映射以呈现子复选框的方法中,您可以为输入标记设置条件语句,仅当 this.state.parentCheckbox 为真时才会呈现它 - 请参见下面的方法.

    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>
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2018-04-15
        • 2021-12-23
        • 2021-08-23
        • 1970-01-01
        相关资源
        最近更新 更多