antd如何获取表单的值_如何在antd中获取FormItem更改时的字段值

I am having a hard time with antd's form.

I have this select field in this form and I want to get the value from it onChange but somehow not getting it to work properly.

say this is the item for which I want the values

{...formItemLayout}

label={fieldLabels.qcategoryid}

validateStatus={categoryError ? "error" : ""}

help={categoryError || ""}

>

{getFieldDecorator("qcategoryid", {

rules: [{ required: true, message: "Please select Category!" }],

onChange: this.handleCategoryChange

})({categoryOptions})}

this is the categoryOptions

if (this.props.categories) {

categoryOptions = this.props.categories.map(item => (

key={item.categoryid}

value={item.categoryid}

name={item.categoryname}

>

{item.categoryname}

));

}

I want both the name of the category and the id

so I created a handleCategoryChange which gets called onChange

and I am able to get the fields I want.

But, it seems that now, I have to click twice on the field to properly select it.

If I click it just once then it does show up in the console. but the field on the form still remains empty. when I click it again, then the field shows up in the form too.

So, what am I doing wrong here.

Ah,yes! Here's the handleCategoryChange function

handleCategoryChange = (value, e) => {

console.log("value is : ", value);

console.log("e : ", e);

this.props.form.setFieldsValue({ qcategoryid: value });

this.setState({

categorySelected: value,

categoryname: e.props.name

});

};

Just to make myself clear.

I need those values before I click submit.

not on submit.

解决方案

Try this:

{...formItemLayout}

label={fieldLabels.qcategoryid}

validateStatus={categoryError ? "error" : ""}

help={categoryError || ""}

>

{getFieldDecorator("qcategoryid", {

rules: [{ required: true, message: "Please select Category!" }]

})({categoryOptions})}

And on the handleCategoryChange function

handleCategoryChange = (value, e) => {

this.setState({

categorySelected: value,

categoryname: e.props.name

});

};

Basically, changing the onChange from the getFieldDecorator helper to the Select, so it doesn't mess with antd's natural behavior, but gets the value and change on state

I've based this answer on the code to the registration form on their website. Specifically, the handleWebsiteChange function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值