javascript - Preventing form from submitting multiple times - Stack Overflow

I've attempted using vuejs to create a form which ultimately uses axios to do a POST request. Howe

I've attempted using vuejs to create a form which ultimately uses axios to do a POST request. However, the submit button does not disable during form validation. I am trying to prevent the user from submitting multiple times…

Code to reproduce:

<div>
    <form @submit.prevent="checkForm">
    <input
        type="submit"
        :disabled="submitting"
        :value="value"
    >
</div>

Where:

checkForm( e ) {
    this.submitting = true;
    this.value = 'Submitting';

    // Form Validation

    this.submitting = false;
    this.value = 'Submit';
}

I've attempted using vuejs to create a form which ultimately uses axios to do a POST request. However, the submit button does not disable during form validation. I am trying to prevent the user from submitting multiple times…

Code to reproduce:

<div>
    <form @submit.prevent="checkForm">
    <input
        type="submit"
        :disabled="submitting"
        :value="value"
    >
</div>

Where:

checkForm( e ) {
    this.submitting = true;
    this.value = 'Submitting';

    // Form Validation

    this.submitting = false;
    this.value = 'Submit';
}
Share Improve this question asked Sep 3, 2018 at 20:09 JoshJosh 2,4443 gold badges19 silver badges35 bronze badges 11
  • Does the button text change when you submit the form? If not, you should probably be using puted properties returning the values of this.submitting and this.value. – Timo Commented Sep 3, 2018 at 20:17
  • No, the text does not change. Can you please provide an example? – Josh Commented Sep 3, 2018 at 20:20
  • You can't really prevent the user from doing anything with client side code. – Luca Kiebel Commented Sep 3, 2018 at 20:35
  • vuejs/v2/guide/puted.html is the documentation for puted properties – Timo Commented Sep 3, 2018 at 20:50
  • Simple example: puted: { disabledSubmit: function () { return this.submitting }, buttonText: function() { return this.submitting ? 'Submitting' : 'Submit'; } } – Timo Commented Sep 3, 2018 at 20:51
 |  Show 6 more ments

1 Answer 1

Reset to default 6

From the ments, it sounds like you're making an asynchronous request (in axios.post()) and immediately setting this.submitting and this.value without awaiting the result of the network request, similar to this example:

checkForm( e ) {
  this.submitting = true;
  this.value = 'Submitting';

  axios.post('https://foo');

  this.submitting = false;
  this.value = 'Submit';
}

Since axios.post() is asynchronous (i.e., returns a Promise), the lines that follow are executed before the POST request even occurs. You can either move those settings into the then callback of axios.post():

checkForm( e ) {
  this.submitting = true;
  this.value = 'Submitting';

  axios.post('https://foo').then(() => {
    this.submitting = false;
    this.value = 'Submit';
  });
}

Or you can use async/await like this:

async checkForm( e ) {
  this.submitting = true;
  this.value = 'Submitting';

  await axios.post('https://foo');

  this.submitting = false;
  this.value = 'Submit';
}

demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745156124a4614130.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信