简体   繁体   中英

Form submit is not prevented when trying to integrate VueJS with SemanticUI

I'm trying to integrate SemanticUI with Vue and I have a Login component with a form in it.

I've added v-on:submit.prevent="onSubmit" to the form but when I press enter on one of the form's fields the form still submits and my method ie never called.

Basically what I'm trying to do is replicate the on("submit" event from jQuery.

Any idea what's the problem?

main.js

import Vue       from "vue";
import VueRouter from "vue-router";
import App       from "./App";

Vue.use(VueRouter);

new Vue({
    el        : "#app",
    template  : "<App/>",
    components: { App }
});

and Login.vue

<template>
    <div class="ui middle aligned center aligned grid">
        <div class="column">
            <h2 class="ui teal header">
                Login
            </h2>

            <form class="ui large form login" v-on:submit.prevent="onSubmit">
                <div class="ui stacked segment">
                    <div class="field">
                        <div class="ui left icon input">
                            <i class="cloud icon"></i>
                            <input type="text" name="hostname" placeholder="Hostname" value="">
                        </div>
                    </div>

                    <div class="field">
                        <div class="ui left icon input">
                            <i class="user icon"></i>
                            <input type="text" name="username" placeholder="Username" value="">
                        </div>
                    </div>

                    <div class="field">
                        <div class="ui left icon input">
                            <i class="lock icon"></i>
                            <input type="password" name="password" placeholder="Password" value="">
                        </div>
                    </div>

                    <div class="ui fluid large teal submit button">Login</div>
                </div>
            </form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "login",

        mounted: function() {
            this.$nextTick(function() {     
                jQuery(this.$el).find("form").form({
                    fields: {
                        hostname: {
                            identifier: "hostname",
                            rules     : [{
                                type  : "empty",
                                prompt: "Please enter the instance hostname"
                            }]
                        },

                        username: {
                            identifier: "username",
                            rules     : [{
                                type  : "empty",
                                prompt: "Please enter your username"
                            }]
                        },

                        password: {
                            identifier: "password",
                            rules     : [{
                                type  : "empty",
                                prompt: "Please enter your password"
                            }]
                        }
                    }
                });
            });
        },

        methods: {
            onSubmit: function(e) {
                alert("ok");
            }
        }
    };
</script>

A form will submit on ENTER key if it has one of the following:

  • a button inside, without type="button"
  • an input tag with type="submit"

Your form does not seem to have either of the above. But assuming you have a submit button elsewhere, here is how you can avoid form submit:

<form onsubmit="return false;">
    <input type="text" placeholder="Host Name" v-model="hostName">
    <input type="text" placeholder="User Name" v-model="userName">
    <input type="password" placeholder="Password" v-model="password">
    <input type="submit" value="Submit form">
</form>

Note that the form has onsubmit="return false;" which prevents accidental submission.

This does not stop you from using a method in Vue component, initiated via a button using @click="submitLoginForm()" , to do the submission using this.$http.post(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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