javascript - SelectField 的 getValue()

标签 javascript meteor reactjs material-ui

我只想获取公司的名称并将其保存到数据库

我正在尝试使用文本做,并尝试使用下面的代码来获取

sport: this.refs.company.getValue(), 并且运行完美。

然后我决定使用SelectField from Material UI对于公司

并且它没有获取数据。

constructor(props) {
super(props);
this.state = {
  value: 1,
};
}

submitResume(event) {
    event.preventDefault();
    console.log(this.state.company.getValue({value}));

CreatePost.insert({
        company: this.refs.company.getValue(),

        employee: this.refs.employee.getValue(),

    });
    console.log("Employee Profile Submitted!");
}


handleNameChange = (event, index, value) => this.setState({value});

    <form onSubmit={this.submitResume.bind(this)}>

          <SelectField
                    floatingLabelText="Company Name"
                    className="validate"
                    ref="company"
                    id="company"
                    floatingLabelStyle={{fontSize:20}}
                    fullWidth={true}
                    value={this.state.value}
                    onChange={this.handleNameChange}
                  >
                    <MenuItem value={1} primaryText="Google" />
                    <MenuItem value={2} primaryText="Facebook" />
                    <MenuItem value={3} primaryText="Amazon" />
                    <MenuItem value={4} primaryText="Nest" />
                    <MenuItem value={5} primaryText="Microsoft" />
            </SelectField>

            <TextField
                className="validate"
                type="text"
                ref="employee"
                id="employee"
                hintText="Enter Full Name"
                floatingLabelStyle={{fontSize:20}}
                floatingLabelText="Employee Name"
                multiLine={true}
                rows={1}
                fullWidth={true}
            />

它对于 TextField 工作正常,但对于 SelectField 则不然。

最佳答案

现在看看这个更新的 您将在参数 valueindexevent

之一中获取值
constructor(props) {
super(props);
this.state = {
  value: 1,
};
}

submitResume(event) {
    event.preventDefault();
    console.log(this.state.company.getValue({value}));

CreatePost.insert({
        company: this.refs.company.getValue(),

        employee: this.refs.employee.getValue(),

    });
    console.log("Employee Profile Submitted!");
}


handleNameChange(event, index, value) {
  console.log(event);
  console.log(index);
  console.log(value);
  this.setState({value});
}

    <form onSubmit={this.submitResume.bind(this)}>

          <SelectField
                    floatingLabelText="Company Name"
                    className="validate"
                    ref="company"
                    id="company"
                    floatingLabelStyle={{fontSize:20}}
                    fullWidth={true}
                    value={this.state.value}
                    onChange={this.handleNameChange}
                  >
                    <MenuItem value={"Google"} primaryText="Google" />
                    <MenuItem value={"Facebook"} primaryText="Facebook" />
                    <MenuItem value={"Amazon"} primaryText="Amazon" />
                    <MenuItem value={"Nest"} primaryText="Nest" />
                    <MenuItem value={"Microsoft"} primaryText="Microsoft" />
            </SelectField>

            <TextField
                className="validate"
                type="text"
                ref="employee"
                id="employee"
                hintText="Enter Full Name"
                floatingLabelStyle={{fontSize:20}}
                floatingLabelText="Employee Name"
                multiLine={true}
                rows={1}
                fullWidth={true}
            />

关于javascript - SelectField 的 getValue(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44939120/

相关文章:

javascript - 窗口 ["name"] 和 javascript 中的 window.name

javascript - Meteor.js 我怎么知道我的收藏在客户端准备好了

image - 如何在上传到 cloudinary 之前调整图像大小?

javascript - 循环对象数组中的属性的最有效方法是什么?

javascript - Later.js - 文本解析正常,但间隔不起作用

javascript - 正则表达式优化和最佳实践

javascript - MeteorJS 调用rest api 服务器端

javascript - 使用钩子(Hook)和状态来 react 纯组件

reactjs - 在我的自定义 React hook 中,是否可以编写一个可以动态返回有效的 firebase 查询的函数?

javascript - webpack < 5 默认情况下用于包含 Node.js 核心模块的 Polyfill。这已不再是这种情况