如何在同一表单中使用React.js处理不同文本输入字段的更改

huangapple go评论68阅读模式
英文:

How to handle a change in different text input fields of the same form using React.js

问题

我有一个包含两个输入字段的表单:姓名和成绩。

我想根据哪个字段正在更改来更新属性,而不是创建两个(将会增加)处理每个字段的函数。

以下是当前的代码,它只使用值更新“name”属性:

handleStudentChange(e) {
    console.log(e.target)
    var value = e.target.value
    console.log("value: ", value)
    var field = e.target
    // e.target返回<input id="mark1" class="form-control" type="number" placeholder="Mark 1" value="40">

    this.setState({
        activeStudent: {
            ...this.state.activeStudent,
            name: value
        }
    })
}

可以通过访问e.target.id属性来使field获取目标属性名称,如“name”或“mark1”。有没有一种将field转换为要使用的属性field:value的方法,或者有没有更好的处理整个事情的方法?提前感谢您!

英文:

I have a form containing two fields of input; name and mark

I want to update a property depending on which field is changing, instead of making two (will be increasing) functions to handle each field individually.

Here is the current code which only updates the name property with the value:

	handleStudentChange(e) {
		console.log(e.target)
		var value = e.target.value
		console.log(&quot;value: &quot;, value)
		var field = e.target
		//e.target returns &lt;input id=&quot;mark1&quot; class=&quot;form-control&quot; type=&quot;number&quot; placeholder=&quot;Mark 1&quot; value=&quot;40&quot;&gt;

		this.setState({
			activeStudent: {
				...this.state.activeStudent,
				name: value
			}
		})
	}

Field can take the value of the target property name such as name or mark1 by tapping into the e.target.id property. Is there a way to cast field as the property to use field:value or any other better way of going about the whole thing? Thanks in advance 如何在同一表单中使用React.js处理不同文本输入字段的更改

答案1

得分: 1

handleChange = (e) => {
const { name, value } = e.target;
this.setState({ ...activeStudent, [name]: value });
};

要使用这种方法,你应该将输入的 name 属性设置为状态属性。例如,如果 activeStudent 有 "name" 属性,相应的输入应该设置为

<input
value={this.state.activeStudent.name}
onChange={handleChange}
type="text"
// 这是重要的部分
name="name"
id="name"
/>

英文:
handleChange = (e) =&gt; {
   const { name, value } = e.target;
   this.setState({ ...activeStudent, [name]: value });
};

the important thing to use this approach, you should have input name prop set as the state property. for example, if activeStudent has "name" property, corresponding input should be set

          &lt;input
                value={this.state.activeStudent.name}
                onChange={handleChange}
                type=&quot;text&quot;
                // this is important part
                name=&quot;name&quot;
                id=&quot;name&quot;
                     
          /&gt;

答案2

得分: 0

你可以为键值设置一个属性。

<input id="mark1" class="form-control" type="number" placeholder="Mark 1" value="40" key="mark" />
<input id="name1" class="form-control" placeholder="Name 1" value="Test Name" key="name" />

然后,你可以通过调用 e.target.getAttribute('key') 来获取该属性。

handleStudentChange(e) {
    var value = e.target.value;
    var key = e.target.getAttribute('key');

    this.setState({
        activeStudent: {
            ...this.state.activeStudent,
            [key]: value
        }
    });
}
英文:

You can set a attribute for key value.

&lt;input id=&quot;mark1&quot; class=&quot;form-control&quot; type=&quot;number&quot; placeholder=&quot;Mark 1&quot; value=&quot;40&quot; key=&quot;mark&quot; /&gt;
&lt;input id=&quot;name1&quot; class=&quot;form-control&quot; placeholder=&quot;Name 1&quot; value=&quot;Test Name&quot; key=&quot;name&quot; /&gt; 

Then you can get that attribute by calling e.target.getAttribute('key').

handleStudentChange(e) {
    var value = e.target.value
    var key = e.target.getAttribute(&#39;key&#39;)

    this.setState({
        activeStudent: {
            ...this.state.activeStudent,
            [key]: value
        }
    })
}

huangapple
  • 本文由 发表于 2023年2月24日 11:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定