如何根据提供的输入阻止表单不提交

时间:2019-01-27 18:05:40

标签: javascript node.js forms input mithril.js

所以我有自己创建的表格。当我输入“无法登录标题或说明”部分后,该表单将不会提交,否则将提交。我不太确定为什么会这样。到目前为止,我的代码如下:

const m = require('mithril')

class IssueEditor {
  constructor(vnode) {
    this.title = vnode.attrs.title
    this.descriptionText = vnode.attrs.descriptionText
    this.onSubmit = vnode.attrs.onSubmit
  }
  view() {
    return m('form', {onsubmit: e => this.onSubmit({title: this.title, descriptionText: this.descriptionText})}, [
      m('.form-group', [
        m('label', {'for': 'title-input'}, 'Issue Title'),
        m('input.form-control#title-input', {value: this.title, oninput: (e) => {this.title = e.target.value}})
      ]),
      m('.form-group', [
        m('label', {'for': 'description-input'}, 'Description'),
        m('textarea.form-control#description-input', {oninput: (e) => {this.descriptionText = e.target.value}}, this.descriptionText)
      ]),
      m('button.btn.btn-primary#save-button', {type: 'submit'}, 'Save')
    ])
  }
}

1 个答案:

答案 0 :(得分:0)

在您的onsubmit函数中,您可能需要一个e.preventDefault()来阻止Javascript自动提交表单,而不是使用vnode.attrs.onSubmit函数。

所以改变

{onsubmit: e => this.onSubmit({title: this.title, descriptionText: this.descriptionText})}, [

{ onsubmit: e => {
    e.preventDefault()
    this.onSubmit({ title: this.title, descriptionText: this.descriptionText })
}}, [