简体   繁体   English

如果插入“select”,则为 select 设置默认值

[英]Set default value for select if `select` is inserted

I have a form with fields.我有一个带有字段的表格。 If RiskType field is ILI and at the same time 'Limit Type' field is I or Y, then The Settlement drop down list is inserted my question is how to make the.netSettlement variable set value the first option in select. If this form has been inserted in the DOM (logically, if the form is not inserted, the value is not set)如果RiskType字段是ILI并且同时“限制类型”字段是 I 或 Y,则插入Settlement下拉列表我的问题是如何使 .netSettlement 变量设置值成为 select 中的第一个选项。如果此表格有已插入到 DOM 中(从逻辑上讲,如果未插入表单,则未设置值)

    {(riskType === 'ILI' && (limitType === 'Y' || limitType === 'I')) &&
      <Form.Row>
        <Form.Group controlId='formGridNetSettlement'>
          <Form.Label>Settlement</Form.Label>
          <Form.Control name='netSettlement' as='select' value={netSettlement}
                        onChange={e => {setNetSettlement(e.currentTarget.value)} }
                        required={!isSearch}>
            <option>True</option>
            <option>False</option>
          </Form.Control>
        </Form.Group>
      </Form.Row>
    }

You'll want to add the selected attribute to the option that you want pre-selected when the form is loaded:您需要将selected属性添加到您希望在加载表单时预先选择的选项:

<option selected value="true">True</option>
<option value="false">False</option>

Also, you will want to set the value attribute with the actual value you want to submit.此外,您还需要将value属性设置为您要提交的实际值。

You do not have values in options at all.您根本没有选项值。 If you're receiving Boolean value for netSettlement try this如果您收到 Boolean 的netSettlement值试试这个

{
  riskType === "ILI" && (limitType === "Y" || limitType === "I") && (
    <Form.Row>
      <Form.Group controlId="formGridNetSettlement">
        <Form.Label>Settlement</Form.Label>
        <Form.Control
          name="netSettlement"
          as="select"
          value={netSettlement.toString()}
          onChange={(e) => {
            setNetSettlement(e.currentTarget.value);
          }}
          required={!isSearch}
        >
          <option value="true">True</option>
          <option value="false">False</option>
        </Form.Control>
      </Form.Group>
    </Form.Row>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM