简体   繁体   English

隐藏已在 select 框中选择的选项 ReactJS

[英]Hiding options already selected in a select box ReactJS

I have 3 select boxes all populated with the same option values.我有 3 个 select 框都填充了相同的选项值。 It is for a user to set security questions, so the user selects each question they desire from each select box and answers it.用户可以设置安全问题,因此用户从每个 select 框中选择他们想要的每个问题并回答它。 I made an API call to receive the security questions and stored them in an array called "securityQuestions. Now what I want to achieve is that when a user selects a question from any of the dropdown, the question selected is hidden from the rest of the select boxes, to avoid the user answering the same question twice. This is how I am displaying the questions and receiving the user's input我做了一个 API 调用来接收安全问题并将它们存储在一个名为“securityQuestions”的数组中。现在我想要实现的是,当用户从任何下拉列表中选择一个问题时,选择的问题从 rest 中隐藏select 框,以避免用户两次回答同一个问题。这就是我显示问题和接收用户输入的方式

<div className="form-group">
                <div className="col-sm-12 col-md-8 col-lg-8">          
                  <select 
                    className="form-control"
                    onChange = {onChange}
                    name = "question1Id"
                    required
                  >

                  <option value="">Select first security question</option>
                    
                        { 
                            securityQuestions ?
                            securityQuestions.map((question, i)=>{
                              return  <option
                                    value={question.id}
                                    key={i}
                                >
                                    {question.name}
                                </option>
                            })
                            :
                            null
                        }
                 </select>
                </div>
              </div> 

              <div className="form-group">
                <div className="col-sm-12 col-md-8 col-lg-8">          
                  <input 
                    type="text" 
                    className="form-control" 
                    required="required"
                    placeholder="Answer 1"
                    name="answer1"
                    onChange={onChange}
                  />
                </div>
              </div> 

              <div className="form-group">
                <div className="col-sm-12 col-md-8 col-lg-8">          
                  <select 
                    className="form-control"
                    onChange = {onChange}
                    name = "question2Id"
                    required
                  >

                  <option value="">Select second security question</option>
                    
                        { 
                            securityQuestions ?
                            securityQuestions.map((question, i)=>{
                              return  <option
                                    value={question.id}
                                    key={i}
                                >
                                    {question.name}
                                </option>
                            })
                            :
                            null
                        }
                 </select>
                </div>
              </div> 

              <div className="form-group">
                <div className="col-sm-12 col-md-8 col-lg-8">          
                  <input 
                    type="text" 
                    className="form-control" 
                    required="required"
                    onChange={onChange}
                    placeholder="Answer 2"
                    name="answer2"
                  />
                </div>
              </div>

And this is my onChange function:这是我的 onChange function:

    const onChange =(e)=>{
        let name = e.target.name;
        let value = e.target.value;
        setState({
            ...state,
           [ e.target.name]: e.target.value
        })
    }

Please how do I implement it that when an option has been selected from the first select dropdown, it is hidden fron the second select dropdown请我如何实现它,当从第一个 select 下拉列表中选择一个选项时,它隐藏在第二个 select 下拉列表中

One way would be to filter your securityQuestions .一种方法是过滤您的securityQuestions Extremely naively:极其天真:

{ securityQuestions &&
  securityQuestions.map((question, i)=> {
    if (question.id === question1Id || question.id === question2Id) {
      return null;
    }
    return  <option
      value={question.id}
      key={i}
    >
      {question.name}
    </option>
  })
}

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

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