javascript - 尝试根据所选选项显示字段

标签 javascript node.js reactjs material-ui

我有团队和比赛的注册部分,但我想做的是,当用户选择比赛时,我试图显示促销代码字段,并且该字段仅在选择比赛时才会显示,如果选择团队,则不会显示促销字段,提前感谢您的帮助。

        React.Component {
          constructor(props) {
            super(props);
            this.state = {name: '', email: '', password: ''};
          }

          handleChange(event) {
            this.setState({ [event.target.name]: event.target.value });
          }

          handleSelect(event, index, value) {
            this.setState({type: value});
          }

          handleSignup(event) {
            event.preventDefault();
            this.props.dispatch(signup(this.state.name, this.state.email, this.state.password, this.state.promo, this.state.type));
          }



          render() {
            return (
              <div className="login">
                <div className="panel">
                  <div className="body">
                    <legend>Create an account</legend>
                    <SelectField
                      floatingLabelText="User or Admin"
                      value={this.state.type}
                      name="type" id="type"
                      onChange={this.handleSelect.bind(this)}
                    >
                      <MenuItem value={"USER"} primaryText="User" />
                      <MenuItem value={"ADMIN"} primaryText="Admin" />
                    </SelectField>

                    <TextField
                      hintText="Promo Code"
                      name="promo" id="promo" autoFocus
                      value={this.state.code} onChange={this.handleChange.bind(this)}
                      floatingLabelText="Promo Code"
                    />

最佳答案

仅当州类型包含“COMPETITION”时,才有条件地呈现文本字段“Promo Code”:

{ condition ? <YourField.../> : null } 包围字段:

 {this.state.type === 'COMPETITION'
      ? <TextField
             hintText="Promo Code"
             name="promo" id="promo" autoFocus
             value={this.state.code} onChange={this.handleChange.bind(this)}
             floatingLabelText="Promo Code"
        />
      : null}

关于javascript - 尝试根据所选选项显示字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45423352/

相关文章:

node.js - Bash 导出变量但仅适用于当前命令

javascript - 获取 api 时未调用 useEffect 且未更新状态

javascript - 为什么这个数组要这样填充?

javascript - 学习者问题 : Functions for every text field. 有没有办法制作一个可以处理多个文本字段的通用函数

javascript - 从中获取动态变量

javascript - 在 ExtJS 网格中重新加载过滤记录时重复未过滤记录

Javascript:为什么我会收到此 Uncaught TypeError:无法读取 null 的属性 'addEventListener'?

javascript - 如何过滤 Angular ng-repeat 以使用日期范围?

node.js - 如何使用参数加载外部文件

javascript - 在 forEach 循环中使用 async/await