【问题标题】:React - HTML Input doesn't disable dynamicReact - HTML 输入不会禁用动态
【发布时间】:2021-08-19 19:10:42
【问题描述】:

我在一个数组中有一个map,并且对于每个项目,我都会渲染一个具有 disabled 属性的组件,其中包含要检查的内容,但是在 input 标记中这不起作用,对于所有渲染的组件,只考虑第一个结果。例如:first_result = false 然后all_results = false

const array = [1, 2, 3, 4, 5];
{array.map((item, index) => (
  <div>
    <input
      id="icon-button-file"
      accept="image/*"
      type="file"
      multiple="multiple"
      onChange={(e) => setState(e.target.files)}
      style={{ display: "none" }}
      disabled={index % 2 === 0}
    />
    <label htmlFor="icon-button-file">
      <IconButton
        color="primary"
        aria-label="upload picture"
        component="span"
        disabled={index % 2 === 0}
      >
        <PhotoCamera />
      </IconButton>
    </label>
  </div>
))}

【问题讨论】:

标签: html reactjs material-ui


【解决方案1】:

嗯,我想通了。 disabled 属性工作正常,但由于某种原因,按钮的 onClick 事件没有触发输入属性中的 onClick 属性。

print of the display: none css property disabled

所以我修改了一点代码:

{array.map((item, index) => (
  <div>
    <input
      id={`icon-button-file-${index}`} 
      accept="image/*"
      type="file"
      multiple="multiple"
      onChange={(e) => setState(e.target.files)}
      style={{ display: "none" }}
      disabled={index % 2 === 0}
    />
    <label htmlFor="icon-button-file">
      <IconButton
        color="primary"
        aria-label="upload picture"
        component="span"
        disabled={index % 2 === 0}
        onClick={() => document.getElementById(`icon-button-file-${item.id}`).click()}
      >
        <PhotoCamera />
      </IconButton>
    </label>
  </div>
))}

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 2011-10-07
    • 2013-01-26
    • 2021-10-23
    • 2019-05-24
    • 2021-03-27
    相关资源
    最近更新 更多