【问题标题】:Form submit is not prevented when trying to integrate VueJS with SemanticUI尝试将 VueJS 与 SemanticUI 集成时不会阻止表单提交
【发布时间】:2016-10-21 08:32:02
【问题描述】:

我正在尝试将 SemanticUI 与 Vue 集成,并且我有一个登录组件,其中包含一个表单。

我已将v-on:submit.prevent="onSubmit" 添加到表单中,但是当我在表单的某个字段上按 Enter 时,表单仍会提交,而我的方法从未调用过。

基本上我想要做的是从 jQuery 复制 on("submit" 事件。

知道有什么问题吗?

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 }
});

和 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>

【问题讨论】:

  • 你能显示你用来提交表单的代码吗?您似乎没有使用标准的 submit 输入。
  • 在这三个输入之一上按 Enter 提交来自。使用标准 jQuery,$(document).on("submit", "form", function(e) { }); 可以工作。
  • 您能否将登录按钮更改为输入并保持样式:&lt;input type="submit" class="ui fluid large teal submit button" value="Login"&gt;
  • @craig_h 哦,天哪,是的。那成功了。 &lt;button type="submit" class="ui fluid large teal submit button"&gt;Login&lt;/button&gt; 看起来还是一样。不知何故,在旧应用程序 SemanticUI 上触发了表单提交表单 JS,即使与 Vue 结合使用的 div 也不起作用。可能有些冲突什么的,目前还不清楚。

标签: javascript vue.js semantic-ui vue-component


【解决方案1】:

如果表单具有以下条件之一,它将按 ENTER 键提交:

  • 一个button在里面,没有type="button"
  • 带有type="submit"input 标签

您的表单似乎没有上述任何一项。但假设您在其他地方有提交按钮,以下是避免表单提交的方法:

<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>

请注意,表单有onsubmit="return false;",可防止意外提交。

这不会阻止您使用 Vue 组件中的方法,通过使用 @click="submitLoginForm()" 的按钮启动,使用 this.$http.post(...) 进行提交

【讨论】:

  • 谢谢!没有 type 属性的按钮将提交表单!我完全忘记了。
猜你喜欢
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 2016-11-26
相关资源
最近更新 更多