在antd Form表单中select设置初始值操作
作者:Jian_Shen_Zero 时间:2024-05-02 17:03:12
我就废话不多说了,大家还是直接看代码吧~
<Form.Item label="作用对象">
{getFieldDecorator('targetId', { initialValue: this.state.targetId }, {
rules: [{
required: false,
message: '作用对象'
}]
})(
<Select placeholder="请选择作用对象">
{targetList.map(entity => <Option key={entity.id} value={entity.id}>{entity.name}</Option>)}
</Select>
)}
</Form.Item>
在getFieldDecorator('targetId',时添加初始值而不是在select标签中添加初始值
补充知识:解决antd 表单获取不到默认值的问题
使用 antd 的 4.x 版本,给表单设置初始值,但是获取不到值
如果需要设置默认值并展示到页面上,需要设置两个地方:
设置 defaultValue,这个是用来展示在页面上的,例如:
<Form.Item label="Sex" name="sex" valuePropName="checked">
//这里的defaultValue
<Radio.Group defaultValue={1}>
<Radio value={1}>男</Radio>
<Radio value={0}>女</Radio>
</Radio.Group>
</Form.Item>
设置 initialValues,这个是用来获取表单值的,例如:
<Form
name="basic"
onFinish={this.onFinish}
onFinishFailed={this.onFinishFailed}
initialValues={{
sex: 1
}}
>
<Form.Item label="Sex" name="sex" valuePropName="checked">
<Radio.Group defaultValue={1}>
<Radio value={1}>男</Radio>
<Radio value={0}>女</Radio>
</Radio.Group>
</Form.Item>
<Form.Item {...tailLayout}>
<Button type="primary" htmlType="submit">
register
</Button>
</Form.Item>
</Form>
来源:https://blog.csdn.net/qq_34756209/article/details/90292136
标签:antd,Form,select,初始值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
cmd运行python文件时对结果进行保存的方法
2023-03-12 03:18:02
![](https://img.aspxhome.com/file/2023/5/92185_0s.jpg)
Python中jieba库的使用方法
2021-06-23 13:47:24
![](https://img.aspxhome.com/file/2023/1/77821_0s.png)
浅谈Python的字典键名可以是哪些类型
2021-06-13 11:39:47
基于Python 装饰器装饰类中的方法实例
2023-10-11 04:10:22
![](https://img.aspxhome.com/file/2023/9/89429_0s.jpg)
Python制作脚本帮女朋友抢购清空购物车
2021-08-30 04:40:01
![](https://img.aspxhome.com/file/2023/3/90923_0s.png)
Python基于socket模块实现UDP通信功能示例
2023-08-25 06:45:27
![](https://img.aspxhome.com/file/2023/7/62027_0s.png)
PHP利用ChatGPT实现轻松创建用户注册页面
2023-05-25 09:22:16
python多线程扫描端口示例
2022-06-28 23:18:58
python打印日志方法的使用教程(logging模块)
2023-12-10 01:39:14
![](https://img.aspxhome.com/file/2023/6/86576_0s.png)
MySQL主从复制的原理及配置方法(比较详细)
2024-01-28 18:21:02
Linux 修改Python命令的方法示例
2023-10-04 18:16:58
![](https://img.aspxhome.com/file/2023/4/128444_0s.jpg)
Django静态资源部署404问题解决方案
2021-05-20 21:28:46
![](https://img.aspxhome.com/file/2023/9/90449_0s.png)
TensorFlow绘制loss/accuracy曲线的实例
2022-01-25 08:01:16
![](https://img.aspxhome.com/file/2023/0/95170_0s.jpg)
使用Python实现牛顿法求极值
2021-10-14 15:10:21
![](https://img.aspxhome.com/file/2023/7/131357_0s.jpg)
Python产生batch数据的操作
2022-11-22 16:00:59
Python Numpy之linspace用法说明
2021-10-17 10:49:55
![](https://img.aspxhome.com/file/2023/3/113173_0s.png)
python中filter,map,reduce的作用
2023-12-18 11:13:15
Python netmiko模块的使用
2021-07-04 06:42:51
Pycharm2020.1安装无法启动问题即设置中文插件的方法
2021-02-03 19:28:29
![](https://img.aspxhome.com/file/2023/2/92132_0s.png)
php完全过滤HTML,JS,CSS等标签
2023-10-09 08:07:34