【问题标题】:Conditionally make an input required in React有条件地输入 React 所需的输入
【发布时间】:2019-05-12 10:08:13
【问题描述】:

在 My React 应用程序的表单中,如果类别设置为滚动,我想制作一组输入(长度和规格)必需。我该怎么做?提前致谢.

 <Form.Group as={Col}>
   <label>Category</label>
   <Form.Control
     as="select"
     name="category"
     defaultValue={this.state.category}
     onChange={this.catControl}
   >
    <option>printed</option>
    <option>roll</option>
  </Form.Control>
</Form.Group>

<Form.Row>
 <Form.Group as={Col}>
   <label>Length(cm)</label>
   //required if category is set to "roll". How can I do that?
   <Form.Control name="length" defaultValue={this.state.length} />
 </Form.Group>

 <Form.Group as={Col}>
   <label>Gauge(mm)</label>
   <Form.Control name="gauge" defaultValue={this.state.gauge} />
 </Form.Group>
</Form.Row>

【问题讨论】:

    标签: reactjs forms required


    【解决方案1】:

    在 html 部分,您可以执行以下操作:

     <Form.Control name="length" defaultValue={this.state.length} required={ this.state.category==='roll'}/>
    

    【讨论】:

      猜你喜欢
      • 2012-03-21
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2022-08-07
      • 2014-09-16
      • 1970-01-01
      相关资源
      最近更新 更多