简体   繁体   English

如何有条件地禁用 React JS 中表单选择中的选项?

[英]How do I conditionally disable an option in a form select in React JS?

So I have been trying to give three sets of options that are rendered from the same data table, and if one option is selected in the first form select, then it should be disabled in the second and third form select.所以我一直在尝试给出从同一个数据表中呈现的三组选项,如果在第一个表单选择中选择了一个选项,那么在第二个和第三个表单选择中应该禁用它。 The three form select options are rendered using a for loop as shown and the三个表单选择选项使用如图所示的 for 循环呈现,

my handleChange method and the final rendered Form Select is written like so:我的 handleChange 方法和最终呈现的 Form Select 是这样写的:

handleChange(e) {
    this.setState({ [e.target.name]: e.target.value });
    for (var i = 0; i < this.state.data.length; i++) {
        if (this.state.data[i].securityQuestion === e.target.value) {
            if (e.target.name === "questionOne") {
                this.setState({
                    questionOneId: this.state.data[i].securityQuestionId
                })
            } else if (e.target.name === "questionTwo") {
                    alert(i)
                this.setState({
                    questionTwoId: this.state.data[i].securityQuestionId
                })
            }
        } else {
            this.setState({
                questionThreeId: this.state.data[i].securityQuestionId
            })
        }
    }
}

<FormGroup>
    <FormSelect onChange={this.handleChange} name="questionOne" value={this.state.questionOne}>
        {this.state.data && this.state.data.map((item, index) =>

            <option key={item.securityQuestionId}>{item.securityQuestion}</option>)
        }
    </FormSelect>
</FormGroup>
<FormGroup>
    <FormInput id="feInputAddress8"
        name="answerOne"
        value={this.state.answerOne}
        invalid={this.state.answerOne === ""}
        valid={this.state.answerOne !== undefined}
        onChange={this.handleChange}
        placeholder="Answer.." />
    <FormFeedback> Answer is mandatory.</FormFeedback>
</FormGroup>

<FormGroup>
    <FormSelect onChange={this.handleChange} name="questionTwo" value={this.state.questionTwo} >
        {this.state.data && this.state.data.map((item, index) =>
            <option key={item.securityQuestionId} onClick={() => this.getQuestionData(item.securityQuestionId, item.securityQuestion)}>{item.securityQuestion}</option>)
        }
    </FormSelect>
</FormGroup>
<FormGroup>
    <FormInput id="feInputAddress8"
        name="answerTwo"
        value={this.state.answerTwo}
        invalid={this.state.answerTwo === ""}
        valid={this.state.answerTwo !== undefined}
        onChange={this.handleChange}
        placeholder="Answer.." />
    <FormFeedback> Answer is mandatory.</FormFeedback>
</FormGroup>

<FormGroup>
    <FormSelect onChange={this.handleChange} name="questionThree" value={this.state.questionThree}>
        {this.state.data && this.state.data.map((item, index) =>
            <option key={item.securityQuestionId}>
                {item.securityQuestion}
            </option>)
        }
    </FormSelect>
</FormGroup>

<FormGroup>
    <FormInput id="feInputAddress8"
        name="answerThree"
        value={this.state.answerThree}
        invalid={this.state.answerThree === ""}
        valid={this.state.answerThree !== undefined}
        onChange={this.handleChange}
        placeholder="Answer.." />
    <FormFeedback> Answer is mandatory.</FormFeedback>
</FormGroup>

if you add a chosen: false param to your item in your data state you can then set the state to data.item[x].chosen = true when an option is chosen, this will rerender the page.如果在数据状态中向项目添加data.item[x].chosen = true chosen: false参数,则可以在选择选项时将状态设置为data.item[x].chosen = true ,这将重新呈现页面。

in the render you can write your option like this:在渲染中,您可以像这样编写您的option

<option key={...} onClick={...} disabled={item.chosen} >{...}</option>

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

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