为什么这个函数不能阻止它的父元素?

时间:2016-12-08 03:23:24

标签: javascript function

如果给出了条件,我创建了一个停止其父级的函数:

handleUserSubmit () {
  this.userForm.options.hasFormSubmitted = true
  if (!this.userForm.options.isFormValid) return
},

handleUpdateUser () {
  const fields = this.userForm.schema
  this.userInput.buildId = this.user.objectId
  this.handleUserSubmit()
  // rest of code
}

然而,无论条件如何,其余代码都会运行。我做错了什么?

1 个答案:

答案 0 :(得分:1)

将条件返回移动到需要从中返回的函数:

handleUserSubmit () {
  this.userForm.options.hasFormSubmitted = true
  return !this.userForm.options.isFormValid;
},

handleUpdateUser () {
  const fields = this.userForm.schema
  this.userInput.buildId = this.user.objectId
  if(this.handleUserSubmit()) return;
  // rest of code
}