0

Clicking any button on the html form triggers a submit event. I am using vue 3's composition API to submit data to a backend on when the submit event is fired

import axios from 'axios'
import { ref } from 'vue'

export default {
   name: "CreateUser",
   
   setup() {
      let username = ref("")
      let password = ref("")
      let email = ref("")

      const submit() => {
         axios.post("api/users/", {
            'username': username.value,
            'email': email.value,
            'password': password.value
})
      }

      const doSomething = () => {}

      return {
         submit,
         username,
         password,
         email,
         doSomething,
      }
   }
}
<form @submit.prevent="submit">
  <label for="username">Username:</label>
  <input type="text" id="username" v-model="username"><br><br>

  <label for="email">Email</label>
  <input type="email" id="email" v-model="email"><br><br>  

  <button @click="doSomething">Do Something</button>

  <label for="password">Password</label>
  <input type="text" id="password" v-model="password"><br><br> 

  <input type="reset" value="Reset">
  <input type="submit" value="Submit">
</form>

The Do Something button also triggers a submit event. There are other input fields below the Do Something button. I am quite new to web development and find this quite confusing. The code added has been simplified. Any explanation would be greatly appreciated as I haven't found any explanation upon searching. Thank you

ayitinya
  • 63
  • 1
  • 10
  • You have to work on your search skills then. [VueJS Button in Form defaults to type="submit"](https://stackoverflow.com/questions/40149152/vue-button-on-click-submits-a-form) In order to change that behaviour specify the type to something other than "submit" – D.Schaller Dec 01 '21 at 08:00
  • Yes that does it. I'd work on my search skills – ayitinya Dec 02 '21 at 04:51

0 Answers0