简体   繁体   English

如何在没有 document.getElementById('..') 的情况下获取输入(文本)字段的值

[英]How to get a value of an input (text) field without document.getElementById('..')

I have an webapplication, written in React.我有一个用 React 编写的 web 应用程序。 I have a mapped(,) component.我有一个 mapped(,) 组件。 which represents an array of states: This is the mapped child component:它代表一个状态数组:这是映射的子组件:

<div className="summarized_content_div">
                    <ul>
                        <li>Benutzername: <input id='inputBenutzername' type="text" defaultValue={this.props.benutzername}/></li>
                        <li>Vorname: <input id='input_vorname' type="text" defaultValue={this.props.vorname}/></li>
                        <li>Nachname: <input id='input_nachname' type="text" defaultValue={this.props.nachname}/></li>
                        <li>E-Mail: <input id='input_email' type="text" defaultValue={this.props.email}/></li>
                        <li>Letzter Login: <input id='input_letzter_login' type="text" defaultValue={this.props.letzter_login}/></li>
                        <li>Status: <input id='input_status' type="text" defaultValue={this.props.status}/></li>
                        <input id="speichern" className="button" type="button" value="Speichern"/><input id="löschen" className="button" type="button" value="Löschen"/>
                    </ul>
                </div>

Now I want the value of the input fields (input_benutzername, input_vorname,...) to be passed to the parent component on a button click.现在我希望在单击按钮时将输入字段(input_benutzername、input_vorname...)的值传递给父组件。 I could to something like:我可以这样做:

const value = document.getElementById('whatever').value

and then do something with that value.然后用那个值做一些事情。

...but as i said, those components are mapped, so it always passes the value of the first mapped component, because it searches for the first element with this id (i guess). ...但正如我所说,这些组件是映射的,所以它总是传递第一个映射组件的值,因为它搜索具有此 id 的第一个元素(我猜)。

So my final question is, how do I get the value of the input fields without using getElementById, because this is not working.所以我的最后一个问题是,如何在不使用 getElementById 的情况下获取输入字段的值,因为这不起作用。

Thanks for you help谢谢你的帮助

EDIT:编辑:

This is the parent component:这是父组件:

doSomethingWithTheValues = (values) => {
...
}


...
<Route path="/verwaltung/benutzer">
                            <div>
                                {this.props.benutzer.map((benutzer, index) => {
                                    return <BenutzerItem
                                        index={index}
                                        key={benutzer.id}
                                        id={benutzer.id}
                                        benutzername={benutzer.benutzername}
                                        vorname={benutzer.vorname}
                                        nachname={benutzer.nachname}
                                        email={benutzer.email}
                                        letzter_login={benutzer.letzter_login}
                                        status={benutzer.status}
                                    />
                                })}
                            </div>
                        </Route>

The way I would do it, is by also passing a function to handle the update:我会这样做的方式是通过 function 来处理更新:

<ExampleForm 
    data={this.state.valuesForExampleForm}
    update={(key,newValue) => this.setState({key, newValue})}
    />

And then in your form you have:然后在你的表格中你有:

const data = this.props.data;
// other code
<li>Benutzername: <input defaultValue={data.benutzername} onChange={(e)=>this.props.update('benutzername', e.target.value)}/></li>

Small disclaimer: It's been a bit since I Reacted, it might be a little off.小免责声明:自从我反应以来已经有点过了,可能有点偏离。

Input field is coded in this way in React.js:输入字段在 React.js 中是这样编码的:

 class Componentname extends React.Component {
 constructor(props) {
 super(props);
 this.state = {value: ''};

 this.handleChange = this.handleChange.bind(this);
 this.handleSubmit = this.handleSubmit.bind(this);
 }

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

 handleSubmit(event) {
 alert('A name was submitted: ' + this.state.value);
 event.preventDefault();
 }

 render() {
  return (
   <form onSubmit={this.handleSubmit}>
     <label>
      Name:
      <input type="text" value={this.state.value} onChange={this.handleChange} 
     />
    </label>
    <input type="submit" value="Submit" />
  </form>
  );
 }
 }

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

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