如何从一个方法中重置Quasar的q-form

huangapple go评论56阅读模式
英文:

How to reset a quasar q-form from a method

问题

我好奇如何在触发提交操作时重置一个q-form。我运行一个名为onSubmit的函数,但不确定如何在该方法中重置q-form,而不必逐个重置每个字段,这很烦人。以下是我的代码:

// 方法
const onSubmit = (event) => { 
  let jsonData = 
  {
    FirstName: firstName.value,
    LastName: lastName.value,
    PhoneNumber: phoneNumber.value,
    EmailAddress: emailAddress.value,
    Message: message.value,
    Token: token.value
  }

  api.post('/api/contactus', jsonData)
    .then((response) => {
      // 在此处理成功的响应
    })
    .catch(() => {
      console.log('API 请求失败')
    })
}
英文:

I am curious how to reset a q-form when the submit action is triggered. I run a function onSubmit but I am not sure how in that method to reset the q-form without having to do each field individually which is annoying. Here is my code:

      //methods
  const onSubmit = (event) => { 
    let jsonData = 
  {
    FirstName: firstName.value,
    LastName: lastName.value,
    PhoneNumber: phoneNumber.value,
    EmailAddress: emailAddress.value,
    Message: message.value,
    Token: token.value
  }

api.post('/api/contactus', jsonData)
.then((response) => {
  
})
.catch(() => {
  console.log('API request failed')
})

}

答案1

得分: 1

The documentation has an example of exactly what you want

// <q-form ref="myForm">

// to reset validations:
function reset () {
  myForm.value.resetValidation()
}

or Options API:

this.$refs.myForm.resetValidation()

This function is shown in the documentation as being tied to a "Reset" button but there's no reason you can't use it after submitting as well.

英文:

The documentation has an example of exactly what you want

// <q-form ref="myForm">

// to reset validations:
function reset () {
  myForm.value.resetValidation()
}

or Options API:

this.$refs.myForm.resetValidation()

This function is shown in the documentation as being tied to a "Reset" button but there's no reason you can't use it after submitting as well.

huangapple
  • 本文由 发表于 2023年2月10日 06:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定