简体   繁体   English

如何在React JS中获取选择框(作为组件)的值?

[英]How to get value of Select Box (which is a component) in react js?

I have created SELECT drop down reusable component, which I am using in a form at different page by importing that component. 我创建了SELECT下拉可重用组件,该组件通过导入该组件在其他页面的表单中使用。 How do i get the value of selection on submit of form? 我如何在提交表格时获得选择的价值?

    import React from 'react';
    export default class Combobox extends React.Component {    
        render() {
            return (

            <div className="combobox-wrapper">
                <select className="form-control">
                {
                    this.props.combolist.map(function(item, i) {
                      return (
                        <option key={i} value={item.name}>{item.name}</option> 
                      ) 
                    })
                  }
                </select>
            </div>
        );
        }

    }

    import React from 'react';
    import Combobox from '../components/Combobox';
    export default class Home extends React.Component {
    submit(event){
            //how to get combobox (Select list) value here
    } 
    render() {
           var comboList = [{name: 'Self'},{name: 'Mother'},
               {name: 'Father'},{name: 'Domestic Partner'}];
            return (
                <div>
                    <div className="col-lg-10 col-md-10 col-sm-10 marginTop20">
                    <form ref="form" onSubmit={this.submit.bind(this)} >
                        <div className="row">
                            <Combobox combolist={comboList} />
                       </div>
        <div className="row">
                            <input type="submit" value="submit"
              className="btn btn-primary" />
                       </div>
    </form>

You need to give your select box a name if you want to reference it using refs. 如果要使用引用引用选择框,则需要为其命名。

https://codepen.io/jzmmm/pen/AXaZPp?editors=0011 https://codepen.io/jzmmm/pen/AXaZPp?editors=0011

Your combobox: 您的组合框:

<Combobox name="mySelect" combolist={comboList} />

In your select component add the name: 在选择组件中添加名称:

<select name={this.props.name} className="form-control">

Then to get the value, your submit function: 然后获取值,您的Submit函数:

  submit(event){
    event.preventDefault();
    console.log(this.refs.form.mySelect.value)
  } 

I would suggest adding custom function PropType which will be called every time user select new option. 我建议添加自定义函数PropType,每次用户选择新选项时都会调用它。

First, add this to your ComboBox. 首先,将其添加到您的ComboBox。

ComboBox.propTypes = {
    onOptionChange: React.PropTypes.func.isRequired
}

Then, make option clickable and let it invoke passed function. 然后,使option可单击,并使其调用传递的函数。 Change: 更改:

<option key={i} value={item.name}>{item.name}</option>

to

<option key={i} value={item.name} onClick={(e) => this.props.onOptionChange(item)>{item.name}</option>

And of course pass function to your ComboBox like this: 当然,将函数传递给ComboBox就像这样:

<ComboBox combolist={combolist} onOptionChange={this.onOptionChange.bind(this)} />

Now your onOptionChange function containing corresponding item will be invoked every time user click any <option> . 现在,每次用户单击任何<option>时,都会调用包含相应项目的onOptionChange函数。 (of course you need to implement it in Home component. (当然,您需要在Home组件中实现它。

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

相关问题 使用道具和状态将选择框的值传递给子组件 React.JS - Using Props And State To Pass Value Of Select Box To Child Component React.JS 如何使用selectize js从选择框中获取数据值 - How to get the data value from a select box using selectize js 如何在 React 中的功能组件中获取 select onChange 的值 - How to Get value of select onChange in Functional Component in React 如何在 Ant Design (Antd), React js 中获取 Dropdown 组件的值 - How to get value of Dropdown component in Ant Design (Antd), React js 如何将状态值从一个组件访问到单独文件中的其他函数(而非组件)? React JS - How to access state value from one component to other function(not component) which is in separate file? React js 如何在反应中显示从addImg.js(子组件)到app.js(父组件)的输入框值 - How to display input box value from addImg.js (child component) to app.js (parent component) in react 从中获取价值<select>React JS 中的(下拉) - Get value from <select> (dropdown) in React JS 如何获取使用循环内的ng-options实现的选择框的值? - How to get the value of a Select box implemented using ng-options which is inside a loop? 如何将组件导入另一个组件中,其中包括反应 js 中的图像 - How to import the component in another component which include image in react js 如何在href标记中获取选择框值 - How to get select box value in a href tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM