K
K
KirylLapouski2018-02-23 21:40:10
JavaScript
KirylLapouski, 2018-02-23 21:40:10

Is it possible, when processing the submit event of the form, to find out the entered data?

The form is in a react component.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton Spirin, 2018-02-24
@KirylLapouski

class Example extends Component {
  state = {
    inputValue: '',
  };

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

  handleSubmit = e => {
    e.preventDefault();
    //  получить состояние формы можно обратившись к this.state
  };

  render() {
    const { inputValue } = this.state;

    return (
      <form onSubmit={this.handleSubmit}>
        <input
          name="inputValue"
          value={inputValue}
          onChange={this.handleChange}
        />
        ...
      </form>
    );
  }

M
Maxim, 2018-02-23
@maxfarseer

Of course you can!
Everything on the page can be pulled through JS. Whether it's a native request, like getElementById, or processing a pre-prepared state, or access via a ref link ...
ps the question is not the most correct, more details are needed.

I
Ilya Kanyshev, 2018-02-23
@ikanyshev

Of course you can!
If pulled from a form, you will need to give the form a name.
So to pull data from the name field of the kek form, you will need to use the following code:
var data = kek.name.value

A
Artem Spiridonov, 2018-02-23
@customtema

On jQuery

$('form').on('submit', function() {
var data = {};
$('form input').each(function() {
data[$(this).attr('name')] = $(this).val();
});
});

console log(data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question