Javascript 当输入被声明为无效时,在React中添加复选框

Javascript 当输入被声明为无效时,在React中添加复选框,javascript,checkbox,reactjs,Javascript,Checkbox,Reactjs,这是我的问题,我有一个列表正在呈现和返回,如下所示: return ( <li className="itemsInList">{this.props.children}</li> ) 返回( {this.props.children} ) 但我想添加一个复选框,因此如果我这样做: return ( <input type="checkbox" className="itemsInList">{this.props.

这是我的问题,我有一个列表正在呈现和返回,如下所示:

return (
        <li className="itemsInList">{this.props.children}</li>
    )
返回(
  • {this.props.children}
  • )
    但我想添加一个复选框,因此如果我这样做:

    return (
            <input type="checkbox" className="itemsInList">{this.props.children}</input>
        )
    
    返回(
    {this.props.children}
    )
    
    但随后会显示一个错误:
    arning:input是一个无效元素标记,不能有子元素


    我怎样才能绕过这个错误,将它们显示在带有复选框的列表中?

    嗯,这个错误很明显。输入不允许有子项。这是HTML定义的一部分。看

    我猜您需要为
    this.props.children
    中的每个项目设置一个复选框。 所以我要做的是保留
  • 并将
    放在里面

    return (
      <ul className="itemsInList">
        {this.props.children.map(function(child) { 
          return (<li className='child'>
            <input type='checkbox'>
            <label>{child.someAttribute}</label>
          </li>);
        }} 
      </ul>
    );
    
    返回(
    
      {this.props.children.map(函数(子){ 返回(
    • {child.someAttribute}
    • ); }}
    );
    请注意,您必须为循环中的每个
  • 指定唯一的
    属性


    希望对您有所帮助。

    嗯,错误很明显。输入不允许有子项。这是HTML定义的一部分。请参阅

    我猜您需要为
    this.props.children
    中的每个项目设置一个复选框。 所以我要做的是保留
  • 并将
    放在里面

    return (
      <ul className="itemsInList">
        {this.props.children.map(function(child) { 
          return (<li className='child'>
            <input type='checkbox'>
            <label>{child.someAttribute}</label>
          </li>);
        }} 
      </ul>
    );
    
    返回(
    
      {this.props.children.map(函数(子){ 返回(
    • {child.someAttribute}
    • ); }}
    );
    请注意,您必须为循环中的每个
  • 指定唯一的
    属性


    希望能有所帮助。

    这对无线电输入类型(一种选择与多种选择)可能吗@ste_irl是的,当然可以。如果你想要“一种选择”,你只需在输入中添加一个
    名称
    属性,对所有输入都一样,就可以创建一个组。这对无线电输入类型(一种选择与多种选择)可能吗@ste_irl是的,当然。如果你想要“一个选择”,你只需在输入中添加一个
    name
    属性,对所有输入都一样,就可以创建一个组。