【问题标题】:ReactJS Checkbox If Value In Array如果值在数组中,则 ReactJS 复选框
【发布时间】:2021-08-27 03:33:48
【问题描述】:

如果值包含在数组中,如何检查 reactjs 中的复选框输入?
我试图在选中的道具中过滤数组,但仍然无法正常工作

  const [productForm, setProductForm] = useState({
    sizes: ["41", "42", "43"],
  });
                [40, 41, 42, 43, 44, 45].map((size, index) => (
                  <div className="col-2" key={index}>
                    <div className="form-group">
                      <div className="form-check form-check-inline">
                        <label className="form-check-label">
                          <input type="checkbox" className="form-check-input" name="sizes" value={size} onChange={e => handleCheckBox(e)} checked={productForm.sizes.includes(size)} />
                          <i className="input-helper"></i>
                          {size}
                        </label>
                      </div>
                    </div>
                  </div>
                ))

【问题讨论】:

    标签: javascript html css reactjs bootstrap-4


    【解决方案1】:

    您的逻辑是正确的,但您正在寻找字符串数组中的数字:

    ["40", "41", "42"].includes(40) // false
    
    [40, 41, 42].includes("40") // false
    
    [40, 41, 42].includes(Number("40")) // true
    

    【讨论】:

    • 啊,我明白了,在像这样将数组中的值转换为与数组中的值相同之后,它现在可以工作了,谢谢...
    猜你喜欢
    • 2020-05-06
    • 1970-01-01
    • 2016-03-04
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多