简体   繁体   English

如何使用 vue.js 2 提交表单?

[英]How to submit a form using vue.js 2?

My view blade laravel like this :我的视图刀片 laravel 是这样的:

<form slot="search" class="navbar-search" action="{{url('search')}}">
    <search-header-view></search-header-view>
</form>

The view blade laravel call vue component (search-header-view component)视图刀片laravel 调用vue 组件(search-header-view 组件)

My vue component(search-header-view component) like this :我的 vue 组件(search-header-view 组件)是这样的:

<template>
    <div class="form-group">
        <div class="input-group">
            <input type="text" class="form-control" placeholder="Search" name="q" autofocus v-model="keyword">
            <span class="input-group-btn">
                <button class="btn btn-default" type="submit" id="submit-search"><span class="fa fa-search"></span></button>
            </span>
            <ul v-if="!selected && keyword">
                <li v-for="state in filteredStates" @click="select(state.name)">{{ state.name }}</li>
            </ul>
        </div>
    </div>
</template>
<script>
    export default {
        name: 'SearchHeaderView',
        data() {
            return {
                baseUrl: window.Laravel.baseUrl,
                keyword: null,
                selected: null,
                filteredStates: []
            }
        },
        watch: {
            keyword(value) {
                this.$store.dispatch('getProducts', { q: value })
                .then(res => {
                    this.filteredStates = res.data;        
                })
            }
        },
        methods: {
            select: function(state) {
                this.keyword = state
                this.selected = state
                document.getElementById('submit-search').submit()
            },
            input: function() {
                this.selected = null
            }
        },    
    }
</script>

I want to submit the form when user click the keyword我想在用户单击关键字时提交表单

I try document.getElementById('submit-search').submit()我尝试 document.getElementById('submit-search').submit()

But on the console exist error like this :但是在控制台上存在这样的错误:

TypeError: document.getElementById(...).submit is not a function类型错误:document.getElementById(...).submit 不是函数

How can I solve this error?我该如何解决这个错误?

You need to call submit() on the <form> element (which is the root element of the component):您需要在<form>元素(它是组件的根元素submit()上调用submit() ):

this.$el.submit();

EDIT: Since you have updated your question, the above no longer applies.编辑:由于您更新了您的问题,因此上述内容不再适用。

To trigger a button click, just call click() on the button element:要触发按钮点击,只需在按钮元素上调用click()

<button ref="submitButton">Foo</button>
this.$refs.submitButton.click();

This is fine if your components are meant to be tightly-coupled, otherwise a cleaner solution would be to $emit an event from your <search-header-view> component and respond to it in the parent component, like this:如果您的组件是紧密耦合的,这很好,否则更简洁的解决方案是从您的<search-header-view>组件中$emit一个事件并在父组件中响应它,如下所示:

this.$emit('submit');

In parent component:在父组件中:

<search-header-view @submit="submit">
methods: {
  submit() {
    // Submit the form manually here (add a ref to your form element)
    this.$refs.form.submit();
  }
}

Just provide the @submit listener to your form :只需为您的表单提供@submit 侦听器:

<form slot="search" class="navbar-search" @submit="submited">
  <search-header-view></search-header-view>
</form>

And the associated method :以及相关的方法:

methods: {

  submited (e) {
    // e is the form event, e.target is the form, do your things on submit
  }
}

In the case where it says that submit is not a function even though you reference it correclt with $refs : It looks like if a form control has a name or id of submit it will mask the form's submit method.如果它说submit is not a function即使您使用$refs正确引用它:看起来如果表单控件的名称或 id 为 submit,它将屏蔽表单的提交方法。 In my case I had name="submit" on my submit button, I removed it and it worked fine.在我的情况下,我的提交按钮上有name="submit" ,我删除了它并且它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM