当前位置: 首页 > news >正文

本章节我们将讨论如何在 React 中使用表单。

React 表单与事件
本章节我们将讨论如何在 React 中使用表单。

HTML 表单元素与 React 中的其他 DOM 元素有所不同,因为表单元素生来就保留一些内部状态。

在 HTML 当中,像 <input>, <textarea>, 和 <select> 这类表单元素会维持自身状态,并根据用户输入进行更新。但在React中,可变的状态通常保存在组件的状态属性中,并且只能用 setState() 方法进行更新。

一个简单的实例
在实例中我们设置了输入框 input 值 value = {this.state.data}。在输入框值发生变化时我们可以更新 state。我们可以使用 onChange 事件来监听 input 的变化,并修改 state。

React 实例
class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'Hello Runoob!'};
this.handleChange = this.handleChange.bind(this);
}

handleChange(event) {
this.setState({value: event.target.value});
}
render() {
var value = this.state.value;
return <div>
<input type="text" value={value} onChange={this.handleChange} />
<h4>{value}</h4>
</div>;
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<HelloMessage />
);

尝试一下 »
上面的代码将渲染出一个值为 Hello Runoob! 的 input 元素,并通过 onChange 事件响应更新用户输入的值。

实例 2
在以下实例中我们将为大家演示如何在子组件上使用表单。 onChange 方法将触发 state 的更新并将更新的值传递到子组件的输入框的 value 上来重新渲染界面。

你需要在父组件通过创建事件句柄 (handleChange) ,并作为 prop (updateStateProp) 传递到你的子组件上。

React 实例
class Content extends React.Component {
render() {
return (
<div>
<input type="text" value={this.props.myDataProp} onChange={this.props.updateStateProp} />
<h4>{this.props.myDataProp}</h4>
</div>
);
}
}

class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = { value: 'Hello Runoob!' };
this.handleChange = this.handleChange.bind(this);
}

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

render() {
var value = this.state.value;
return (
<div>
<Content myDataProp={value} updateStateProp={this.handleChange} />
</div>
);
}
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<HelloMessage />);

尝试一下 »
Select 下拉菜单
在 React 中,不使用 selected 属性,而在根 select 标签上用 value 属性来表示选中项。

React 实例
class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'coconut'};

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

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

handleSubmit(event) {
alert('Your favorite flavor is: ' + this.state.value);
event.preventDefault();
}

render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
选择您最喜欢的网站
<select value={this.state.value} onChange={this.handleChange}>
<option value="gg">Google</option>
<option value="rn">Runoob</option>
<option value="tb">Taobao</option>
<option value="fb">Facebook</option>
</select>
</label>
<input type="submit" value="提交" />
</form>
);
}
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<FlavorForm />
);

尝试一下 »
多个表单
当你有处理多个 input 元素时,你可以通过给每个元素添加一个 name 属性,来让处理函数根据 event.target.name 的值来选择做什么。

React 实例
class Reservation extends React.Component {
constructor(props) {
super(props);
this.state = {
isGoing: true,
numberOfGuests: 2
};

this.handleInputChange = this.handleInputChange.bind(this);
}

handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;

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

render() {
return (
<form>
<label>
是否离开:
<input
name="isGoing"
type="checkbox"
checked={this.state.isGoing}
onChange={this.handleInputChange} />
</label>
<br />
<label>
访客数:
<input
name="numberOfGuests"
type="number"
value={this.state.numberOfGuests}
onChange={this.handleInputChange} />
</label>
</form>
);
}
}

尝试一下 »
React 事件
以下实例演示通过 onClick 事件来修改数据:

React 实例
class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'Hello Runoob!'};
this.handleChange = this.handleChange.bind(this);
}

handleChange(event) {
this.setState({value: '菜鸟教程'})
}
render() {
var value = this.state.value;
return <div>
<button onClick={this.handleChange}>点我</button>
<h4>{value}</h4>
</div>;
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<HelloMessage />
);

尝试一下 »
当你需要从子组件中更新父组件的 state 时,你需要在父组件通过创建事件句柄 (handleChange) ,并作为 prop (updateStateProp) 传递到你的子组件上。实例如下:

React 实例
class Content extends React.Component {
render() {
return <div>
<button onClick = {this.props.updateStateProp}>点我</button>
<h4>{this.props.myDataProp}</h4>
</div>
}
}
class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'Hello Runoob!'};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({value: '菜鸟教程'})
}
render() {
var value = this.state.value;
return <div>
<Content myDataProp = {value}
updateStateProp = {this.handleChange}></Content>
</div>;
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<HelloMessage />
);

尝试一下 »
React AJAXReact Refs

1 篇笔记 写笔记
杨笑

117***1030@qq.com

51
父组件和子组件都用表单:

class HelloMessageChild extends React.Component {
render(){
return <div>
<input type="text" value={this.props.myDataProp} onChange={this.props.updateStateProp} />
<h4>子组件显示:{this.props.myDataProp}</h4>
</div>;
}
}
class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = {value: '父组件',value1:"子组件"};
this.handleChange = this.handleChange.bind(this);
this.handleChange1 = this.handleChange1.bind(this);
}

handleChange(event) {
this.setState({value: event.target.value});
}
handleChange1(event) {
this.setState({value1: event.target.value});
}
render() {
var value = this.state.value;
var value1 = this.state.value1;
return <div>
<table><tbody>
<tr>
<td>
<input type="text" value={value} onChange={this.handleChange} />
<h4>父组件显示:{value}</h4>
</td>
<td>
<HelloMessageChild myDataProp = {value1} updateStateProp = {this.handleChange1} />
</td>
</tr>
</tbody></table>
</div>;

rogbbs.asus.com.cn/groups/5/posts/542963
rogbbs.asus.com.cn/groups/5/posts/542966
rogbbs.asus.com.cn/groups/5/posts/542965
rogbbs.asus.com.cn/groups/5/posts/542964
rogbbs.asus.com.cn/groups/5/posts/542967
rogbbs.asus.com.cn/groups/5/posts/542968
rogbbs.asus.com.cn/groups/5/posts/542969
rogbbs.asus.com.cn/groups/5/posts/542970
rogbbs.asus.com.cn/groups/5/posts/542972
rogbbs.asus.com.cn/groups/5/posts/542971
rogbbs.asus.com.cn/groups/5/posts/542973
rogbbs.asus.com.cn/groups/5/posts/542974
rogbbs.asus.com.cn/groups/5/posts/542975
rogbbs.asus.com.cn/groups/5/posts/542976
rogbbs.asus.com.cn/groups/5/posts/542977
rogbbs.asus.com.cn/groups/5/posts/542978
rogbbs.asus.com.cn/groups/5/posts/542979
rogbbs.asus.com.cn/groups/5/posts/542980
rogbbs.asus.com.cn/groups/5/posts/542981
rogbbs.asus.com.cn/groups/5/posts/542982
rogbbs.asus.com.cn/groups/5/posts/542983
rogbbs.asus.com.cn/groups/5/posts/542984
rogbbs.asus.com.cn/groups/5/posts/542986
rogbbs.asus.com.cn/groups/5/posts/542985
rogbbs.asus.com.cn/groups/5/posts/542987
rogbbs.asus.com.cn/groups/5/posts/542989
rogbbs.asus.com.cn/groups/5/posts/542988
rogbbs.asus.com.cn/groups/5/posts/542990
rogbbs.asus.com.cn/groups/5/posts/542991
rogbbs.asus.com.cn/groups/5/posts/542992
rogbbs.asus.com.cn/groups/5/posts/542993
rogbbs.asus.com.cn/groups/5/posts/542994
rogbbs.asus.com.cn/groups/5/posts/542995
rogbbs.asus.com.cn/groups/5/posts/542996
rogbbs.asus.com.cn/groups/5/posts/542997
rogbbs.asus.com.cn/groups/5/posts/542998
rogbbs.asus.com.cn/groups/5/posts/542999
rogbbs.asus.com.cn/groups/5/posts/543001
rogbbs.asus.com.cn/groups/5/posts/543000
rogbbs.asus.com.cn/groups/5/posts/543002
rogbbs.asus.com.cn/groups/5/posts/543003
rogbbs.asus.com.cn/groups/5/posts/543004
rogbbs.asus.com.cn/groups/5/posts/543005
rogbbs.asus.com.cn/groups/5/posts/543006
rogbbs.asus.com.cn/groups/5/posts/543007
rogbbs.asus.com.cn/groups/5/posts/543008
rogbbs.asus.com.cn/groups/5/posts/543009
rogbbs.asus.com.cn/groups/5/posts/543010
rogbbs.asus.com.cn/groups/5/posts/543011
rogbbs.asus.com.cn/groups/5/posts/543013
rogbbs.asus.com.cn/groups/5/posts/543015
rogbbs.asus.com.cn/groups/5/posts/543016
rogbbs.asus.com.cn/groups/5/posts/543012
rogbbs.asus.com.cn/groups/5/posts/543017
rogbbs.asus.com.cn/groups/5/posts/543018
rogbbs.asus.com.cn/groups/5/posts/543019
rogbbs.asus.com.cn/groups/5/posts/543020
rogbbs.asus.com.cn/groups/5/posts/543021
rogbbs.asus.com.cn/groups/5/posts/543022
rogbbs.asus.com.cn/groups/5/posts/543024
rogbbs.asus.com.cn/groups/5/posts/543025
rogbbs.asus.com.cn/groups/5/posts/543027
rogbbs.asus.com.cn/groups/5/posts/543026
rogbbs.asus.com.cn/groups/5/posts/543028
rogbbs.asus.com.cn/groups/5/posts/543029
rogbbs.asus.com.cn/groups/5/posts/543030
rogbbs.asus.com.cn/groups/5/posts/543031
rogbbs.asus.com.cn/groups/5/posts/543033
rogbbs.asus.com.cn/groups/5/posts/543034
rogbbs.asus.com.cn/groups/5/posts/543035
rogbbs.asus.com.cn/groups/5/posts/543037
rogbbs.asus.com.cn/groups/5/posts/543038
rogbbs.asus.com.cn/groups/5/posts/543032
rogbbs.asus.com.cn/groups/5/posts/543036
rogbbs.asus.com.cn/groups/5/posts/543039
rogbbs.asus.com.cn/groups/5/posts/543040
rogbbs.asus.com.cn/groups/5/posts/543042
rogbbs.asus.com.cn/groups/5/posts/543044
rogbbs.asus.com.cn/groups/5/posts/543041
rogbbs.asus.com.cn/groups/5/posts/543043
rogbbs.asus.com.cn/groups/5/posts/543045
rogbbs.asus.com.cn/groups/5/posts/543046
rogbbs.asus.com.cn/groups/5/posts/543047
rogbbs.asus.com.cn/groups/5/posts/543023
rogbbs.asus.com.cn/groups/5/posts/543048
rogbbs.asus.com.cn/groups/5/posts/543049
rogbbs.asus.com.cn/groups/5/posts/543050
rogbbs.asus.com.cn/groups/5/posts/543051
rogbbs.asus.com.cn/groups/5/posts/543052
rogbbs.asus.com.cn/groups/5/posts/543053
rogbbs.asus.com.cn/groups/5/posts/543054
rogbbs.asus.com.cn/groups/5/posts/543055
rogbbs.asus.com.cn/groups/5/posts/543056
rogbbs.asus.com.cn/groups/5/posts/543058
rogbbs.asus.com.cn/groups/5/posts/543057
rogbbs.asus.com.cn/groups/5/posts/543059
rogbbs.asus.com.cn/groups/5/posts/543060
rogbbs.asus.com.cn/groups/5/posts/543061
rogbbs.asus.com.cn/groups/5/posts/543062
rogbbs.asus.com.cn/groups/5/posts/543063
rogbbs.asus.com.cn/groups/5/posts/543065
rogbbs.asus.com.cn/groups/5/posts/543064
rogbbs.asus.com.cn/groups/5/posts/543066
rogbbs.asus.com.cn/groups/5/posts/543068
rogbbs.asus.com.cn/groups/5/posts/543069
rogbbs.asus.com.cn/groups/5/posts/543070
rogbbs.asus.com.cn/groups/5/posts/543071
rogbbs.asus.com.cn/groups/5/posts/543072
rogbbs.asus.com.cn/groups/5/posts/543073
rogbbs.asus.com.cn/groups/5/posts/543074
rogbbs.asus.com.cn/groups/5/posts/543075
rogbbs.asus.com.cn/groups/5/posts/543076
rogbbs.asus.com.cn/groups/5/posts/543077
rogbbs.asus.com.cn/groups/5/posts/543078
rogbbs.asus.com.cn/groups/5/posts/543067
rogbbs.asus.com.cn/groups/5/posts/543079
rogbbs.asus.com.cn/groups/5/posts/543080
rogbbs.asus.com.cn/groups/5/posts/543081
rogbbs.asus.com.cn/groups/5/posts/543082
rogbbs.asus.com.cn/groups/5/posts/543083
rogbbs.asus.com.cn/groups/5/posts/543085
rogbbs.asus.com.cn/groups/5/posts/543084
rogbbs.asus.com.cn/groups/5/posts/543086
rogbbs.asus.com.cn/groups/5/posts/543088
rogbbs.asus.com.cn/groups/5/posts/543087
rogbbs.asus.com.cn/groups/5/posts/543089
rogbbs.asus.com.cn/groups/5/posts/543090
rogbbs.asus.com.cn/groups/5/posts/543091
rogbbs.asus.com.cn/groups/5/posts/543093
rogbbs.asus.com.cn/groups/5/posts/543095
rogbbs.asus.com.cn/groups/5/posts/543094
rogbbs.asus.com.cn/groups/5/posts/543092
rogbbs.asus.com.cn/groups/5/posts/543096
rogbbs.asus.com.cn/groups/5/posts/543098
rogbbs.asus.com.cn/groups/5/posts/543097
rogbbs.asus.com.cn/groups/5/posts/543100
rogbbs.asus.com.cn/groups/5/posts/543099
rogbbs.asus.com.cn/groups/5/posts/543101
rogbbs.asus.com.cn/groups/5/posts/543102
rogbbs.asus.com.cn/groups/5/posts/543103
rogbbs.asus.com.cn/groups/5/posts/543105
rogbbs.asus.com.cn/groups/5/posts/543104
rogbbs.asus.com.cn/groups/5/posts/543107
rogbbs.asus.com.cn/groups/5/posts/543109
rogbbs.asus.com.cn/groups/5/posts/543106
rogbbs.asus.com.cn/groups/5/posts/543110
rogbbs.asus.com.cn/groups/5/posts/543111
rogbbs.asus.com.cn/groups/5/posts/543112
rogbbs.asus.com.cn/groups/5/posts/543113
rogbbs.asus.com.cn/groups/5/posts/543114
rogbbs.asus.com.cn/groups/5/posts/543115
rogbbs.asus.com.cn/groups/5/posts/543116
rogbbs.asus.com.cn/groups/5/posts/543117
rogbbs.asus.com.cn/groups/5/posts/543118
rogbbs.asus.com.cn/groups/5/posts/543108
rogbbs.asus.com.cn/groups/5/posts/543119
rogbbs.asus.com.cn/groups/5/posts/543120
rogbbs.asus.com.cn/groups/5/posts/543121
rogbbs.asus.com.cn/groups/5/posts/543123
rogbbs.asus.com.cn/groups/5/posts/543122
rogbbs.asus.com.cn/groups/5/posts/543124
rogbbs.asus.com.cn/groups/5/posts/543125
rogbbs.asus.com.cn/groups/5/posts/543126
rogbbs.asus.com.cn/groups/5/posts/543127
rogbbs.asus.com.cn/groups/5/posts/543128
rogbbs.asus.com.cn/groups/5/posts/543130
rogbbs.asus.com.cn/groups/5/posts/543129
rogbbs.asus.com.cn/groups/5/posts/543131
rogbbs.asus.com.cn/groups/5/posts/543132
rogbbs.asus.com.cn/groups/5/posts/543133
rogbbs.asus.com.cn/groups/5/posts/543134
rogbbs.asus.com.cn/groups/5/posts/543136
rogbbs.asus.com.cn/groups/5/posts/543135
rogbbs.asus.com.cn/groups/5/posts/543137
rogbbs.asus.com.cn/groups/5/posts/543138
rogbbs.asus.com.cn/groups/5/posts/543139
rogbbs.asus.com.cn/groups/5/posts/543140
rogbbs.asus.com.cn/groups/5/posts/543142
rogbbs.asus.com.cn/groups/5/posts/543141
rogbbs.asus.com.cn/groups/5/posts/543143
rogbbs.asus.com.cn/groups/5/posts/543145
rogbbs.asus.com.cn/groups/5/posts/543144
rogbbs.asus.com.cn/groups/5/posts/543146
rogbbs.asus.com.cn/groups/5/posts/543147
rogbbs.asus.com.cn/groups/5/posts/543148
rogbbs.asus.com.cn/groups/5/posts/543149
rogbbs.asus.com.cn/groups/5/posts/543150
rogbbs.asus.com.cn/groups/5/posts/543151
rogbbs.asus.com.cn/groups/5/posts/543153
rogbbs.asus.com.cn/groups/5/posts/543154
rogbbs.asus.com.cn/groups/5/posts/543155
rogbbs.asus.com.cn/groups/5/posts/543156
rogbbs.asus.com.cn/groups/5/posts/543157
rogbbs.asus.com.cn/groups/5/posts/543158
rogbbs.asus.com.cn/groups/5/posts/543159
rogbbs.asus.com.cn/groups/5/posts/543160
rogbbs.asus.com.cn/groups/5/posts/543161
rogbbs.asus.com.cn/groups/5/posts/543162
rogbbs.asus.com.cn/groups/5/posts/543163
rogbbs.asus.com.cn/groups/5/posts/543167
rogbbs.asus.com.cn/groups/5/posts/543166
rogbbs.asus.com.cn/groups/5/posts/543168
rogbbs.asus.com.cn/groups/5/posts/543170
rogbbs.asus.com.cn/groups/5/posts/543164
rogbbs.asus.com.cn/groups/5/posts/543169
rogbbs.asus.com.cn/groups/5/posts/543171
rogbbs.asus.com.cn/groups/5/posts/543172
rogbbs.asus.com.cn/groups/5/posts/543173
rogbbs.asus.com.cn/groups/5/posts/543174
rogbbs.asus.com.cn/groups/5/posts/543152
rogbbs.asus.com.cn/groups/5/posts/543175
rogbbs.asus.com.cn/groups/5/posts/543177
rogbbs.asus.com.cn/groups/5/posts/543178
rogbbs.asus.com.cn/groups/5/posts/543179
rogbbs.asus.com.cn/groups/5/posts/543181
rogbbs.asus.com.cn/groups/5/posts/543180

}
}
ReactDOM.render(
<HelloMessage />,
document.getElementById('formexmple')
);

http://www.jsqmd.com/news/206338/

相关文章:

  • 专利解析:涂液器凸轮槽与导向突起的滑动配合机制
  • Escrcpy(安卓手机投屏软件)
  • 显微观察:Bamtone K系列盲孔显微镜性能优势深度评测
  • Photo Editor安卓版(照片编辑器安卓版)
  • 利用多智能体AI实现动态竞争格局评估
  • 【高斯泼溅】当3DGS遇上传统模型:从“画在一起”到“画得对”的全攻略​
  • aigc免费降重神器测评:这才是降低ai率的正确打开方式,降ai率必看。
  • leetcode 856. Score of Parentheses 括号的分数-耗时100
  • 如何优化慢SQL?索引失效的常见场景有哪些?
  • 技术视角拆解:一家AI搜索优化服务商的完整方法论——以北京链创网络为例
  • springboot+vue开发实现新能源汽车4s店销售管理系统应用和研究
  • Hadoop数据可视化解决方案
  • 电脑c盘红了怎么清理c盘空间,教你几招解决,马上满血复活!
  • 免费降低ai率看这篇就够了!5款降ai率工具实测,降ai效果炸裂。
  • GitHub精选----从监控到黑苹果:盘点5个让你相见恨晚的硬核开源项目
  • 【免费】AD人员信息自动同步工具|人事变动实时响应,企业账号管理高效协同
  • AI助力巴菲特式护城河分析:多维度评估竞争优势
  • 2026最新实测:降ai率从50%降到10%!ai降ai实操流程分享,论文降ai并不难。
  • 缠三买点涨停基因 条件预警公式
  • 1951-2024年各区县平均风速数据
  • 通达信清新的“精准极品底”公式
  • 麻雀搜索算法优化深度置信网络:SSA-DBN模型解析与代码注释详解,简单上手,轻松掌握
  • miniwiggler连接不上可能出问题得地方
  • 超级宝贝的不带未来函数的主图指标
  • 黑盒测试的底层逻辑
  • 基于Hadoop的黑龙江旅游景点推荐系统的设计与实现(源码+论文+部署+安装)
  • 【无线传感器】基于matlab遗传算法GA无线传感器网络中聚类以增强网络寿命【含Matlab源码 14848期】
  • 解析 ‘Progressive Revelation’:如何在图执行过程中,分阶段向用户展示 Agent 的思考进度?
  • 直接上干货!这个通信信号调制识别数据集生成工具能让你摆脱数据荒,咱们从核心代码开始拆解。先看信号生成器的核心逻辑
  • 深入 ‘Steering the Agent’:利用输入反馈实时改变正在运行中的 Graph 权重,实现‘人机共驾’