React - Preventing Form Submission

前端 未结 11 818
一个人的身影
一个人的身影 2020-11-27 06:35

I\'ve been experimenting with React. In my experiement, I\'m using the Reactstrap framework.When I click a button, I\'ve noticed that the HTML form submits. Is there a way t

相关标签:
11条回答
  • 2020-11-27 07:29
    componentDidUpdate(){               
        $(".wpcf7-submit").click( function(event) {
            event.preventDefault();
        })
    }
    

    You can use componentDidUpdate and event.preventDefault() to disable form submission.As react does not support return false.

    0 讨论(0)
  • 2020-11-27 07:35

    There's another, more accessible solution: Don't put the action on your buttons. There's a lot of functionality built into forms already. Instead of handling button presses, handle form submissions and resets. Simply add onSubmit={handleSubmit} and onReset={handleReset} to your form elements.

    To stop the actual submission just include event in your function and an event.preventDefault(); to stop the default submission behavior. Now your form behaves correctly from an accessibility standpoint and you're handling any form of submission the user might take.

    0 讨论(0)
  • 2020-11-27 07:36

    preventDefault is what you're looking for. To just block the button from submitting

    <Button onClick={this.onClickButton} ...
    

    code

    onClickButton (event) {
      event.preventDefault();
    }
    

    If you have a form which you want to handle in a custom way you can capture a higher level event onSubmit which will also stop that button from submitting.

    <form onSubmit={this.onSubmit}>
    

    and above in code

    onSubmit (event) {
      event.preventDefault();
    
      // custom form handling here
    }
    
    0 讨论(0)
  • 2020-11-27 07:36

    2 ways

    First one we pass the event in the argument right into the onClick.

      onTestClick(e) {
        e.preventDefault();
        alert('here');
      }
    

      // Look here we pass the args in the onClick
      <Button color="primary" onClick={e => this.onTestClick(e)}>primary</Button>&nbsp;
    

    Second one we pass it into argument and we did right in the onClick

      onTestClick() {
        alert('here');
      }
    

      // Here we did right inside the onClick, but this is the best way
      <Button color="primary" onClick={e => (e.preventDefault(), this.onTestClick())}>primary</Button>&nbsp;
    

    Hope that can help

    0 讨论(0)
  • 2020-11-27 07:36
    import React, { Component } from 'react';
    
    export class Form extends Component {
      constructor(props) {
        super();
        this.state = {
          username: '',
        };
      }
      handleUsername = (event) => {
        this.setState({
          username: event.target.value,
        });
      };
    
      submited = (event) => {
        alert(`Username: ${this.state.username},`);
        event.preventDefault();
      };
      render() {
        return (
          <div>
            <form onSubmit={this.submited}>
              <label>Username:</label>
              <input
                type="text"
                value={this.state.username}
                onChange={this.handleUsername}
              />
              <button>Submit</button>
            </form>
          </div>
        );
      }
    }
    
    export default Form;
    
    0 讨论(0)
提交回复
热议问题