Vuejs not delivering anticipated result

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
	<title>Document</title>
</head>
<body>
	<div id="app">
		<div class="container">
			<form v-if="!submitted" class="mt-5">
				<div class="form-group">
					<label for="email">Email</label>
					<input type="email" v-model:value="email" class="form-control form-control-lg">
				</div>
				<button type="submit" @:click.prevent="process" class="btn btn-primary">Submit</button>
			</form>
			<h2 v-if="submitted" class="mt-5">Thanks for signing Up</h2>
		</div>
	</div>
	
	
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script>
	var vm = new Vue({
		el: '#app',
		data: {
			email: 'vonbraun@nasa.com',
			submitted: false
		},
		methods: {
			process: function() {
				// alert('Submitted' + this.email);
				this.submitted = true;
			}
		}
	});
</script>
</body>
</html>

Live URL

The anticipated result → This should have been fired.

<h2 v-if="submitted" class="mt-5">Thanks for signing Up</h2>

shouldnt that be v-on:submit.prevent inside the form tag instead?

It looks to me like you’re not preventing the form from submitting itself, so it reloads the page.

I have made the suggested change, but still seems to be standing at the same locus.

You… sure you’ve made the change? (My browser tells me the form has no event listeners, but the button does… a button does not get submitted does not receive the submit event.)

1 Like

As m_hutley says, you need to move your event handler onto the form tag:

<form @submit.prevent="process" v-if="!submitted" class="mt-5">
  ...
</form>

Also, I find the flow a little hard to parse, as you start with a negated condition:

<form v-if="!submitted" class="mt-5">
  ...
</form>
<h2 v-if="submitted" class="mt-5">Thanks for signing Up</h2>

For me, this would be easier to read:

<h2 v-if="submitted" class="mt-5">Thanks for signing Up</h2>
<form v-else @submit.prevent="process" class="mt-5">
  ...
</form>
1 Like

Hi there, I have not made any changes and quite strange it started to work - The job which was not working has started to work now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.