有条件地将readOnly添加到react表带中的输入

时间:2019-08-30 19:31:47

标签: reactjs reactstrap

根据(Change readonly attribute of input element: ReactJS)。 我正在尝试以下。请注意,我正在使用React Strap,但我认为这没什么不同。

父组件: MemberForm类扩展Component {   构造函数(道具){     超级(道具)

this.state = { readOnly: true, };

}

//尽量减少以仅显示相关代码

render() {
return (
                <InputMemberForm
                  submitMember={this.submitMember}
                  onChange={this.onChange}
                  first={first}
                  last={last}
                  phone={phone}
                  email={email}
                  address={address}
                  address2={address2}
                  city={city}
                  zip={zip}
                  headOfHousehold={headOfHousehold}
                  household={household}
                  readOnly={this.state.readOnly}
                />
)}

如您所见,我以readOnly={this.state.readOnly}的身份传递readOnly

const InputMemberForm = props => (
            <Input
              className="form-control-alternative"
              name="first"
              value={props.first}
              onChange={props.onChange}
              id="input-first-name"
              placeholder="First name"
              type="text"
              readOnly = {props.readOnly}
            />
)

readOnly对输入无效。
然后,我尝试了这个:readOnly = true; 也没有效果。

仅放置readOnly即可使表单变为readOnly。
如果以上方法不起作用,我如何有条件地显示带反应带的readOnly?

0 个答案:

没有答案