Selvin
Selvin

Reputation: 815

React | Dynamic select get values

I'm using ant design for my react project.

In this, I need to add dynamic select and get values. Now I can able to select dropdown dynamically.

But I'm not able to get values.

handleSubmit = e => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        alert("Received values of form: ", values);
      }
    });
  };

Demo: https://codesandbox.io/s/nrxq1505v0

Upvotes: 2

Views: 509

Answers (3)

Vikash_Singh
Vikash_Singh

Reputation: 1896

This will resolve your problem.

this.props.form.validateFields((err, values) => {
  e.preventDefault();
  if (!err) {
    alert("Received values of form: " + values.names.join());
  }
});

Upvotes: 2

Thilina Sampath
Thilina Sampath

Reputation: 3773

Please check all time your variable assign correct variable type or your expected variable using

console.log('comment', variable); 

this is easy way, or you can debug your code

Please check working example link.

Upvotes: 0

Hemadri Dasari
Hemadri Dasari

Reputation: 34004

You need to do like below to get values

this.props.form.validateFields((err, values) => {
      const data = JSON.stringify(values);
      const d = JSON.parse(data);
      console.log("d...", d.names)//this gives values in an array. It prints like this ["lucy", "jack"] when I added two fields and seleted value from each field
      if (!err) {
        alert("Received values of form: ", d.names);
      }
    });

Upvotes: 0

Related Questions