为什么< form>被赋予NoValidate属性? [英] Why is <form> being given NoValidate attribute?

查看:151
本文介绍了为什么< form>被赋予NoValidate属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法让jQuery Validate插件发挥得很好。

Having trouble getting jQuery Validate plugin to play nice.

模型

public class FooVM
{
    [Required]
    public string Name { get; set; }
}

布局

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <script src="@Url.Content("~/scripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/scripts/jquery-migrate-1.0.0.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/scripts/bootstrap.min.js")" type="text/javascript"></script>
        <link href="@Url.Content("~/content/bootstrap-responsive.min.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/content/bootstrap.min.css")" rel="stylesheet" type="text/css" />
        <title>@ViewBag.Title</title>
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="span12">
                <div class="navbar">
                    <div class="navbar-inner">
                        <a class="brand" href="#">idoneit</a>
                        <ul class="nav">
                            <li class="menu-link">@Html.ActionLink("Home", "index", "bar")</li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="span12 error-container">

            </div>
            <div class="span12 main-body">
                @RenderBody()
            </div>
        </div>
    </div>
    </body>
</html>

查看

model bootstrapvalidate.Models.FooVM
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("add", "bar", FormMethod.Post, new { @class = "form-horizontal" }))
{
    <fieldset>
        @Html.ValidationSummary()
        <legend>Testing Bootstrap & Validate</legend>
        <div class="control-group">
            <label for="Name" class="control-label">Name</label>
            <div class="controls">
              @Html.TextBoxFor(x => x.Name) 
            </div>
            <button type="submit" class="btn">Add!</button>
        </div>
    </fieldset>
}

当我提交时,会短暂显示错误消息,然后表单回发无论如何。

When I submit, the error message is briefly shown and then the form posts back anyway.

我注意到的一件事,我之前没见过的是标记包含'novalidate'属性

One thing I have noticed which I have not seen before is that the markup contains the 'novalidate' attribute

<form action="/bar/add" class="form-horizontal" method="post" novalidate="novalidate">    

从我读过的内容来看,这是阻止验证的HTML5属性。所以是的,这就是我所假设的原因。

From the bits I have read, this is HTML5 attribute that prevents validation. So yeah, this is what is causing it I assume.

问题是 - 为什么bejesus会被添加到表单中?我没有要求它!

Question is - why the bejesus is it being added to the form? I've not asked for it!

编辑:基于@rob的答案进行了一些挖掘,似乎jquery validate正在抛出异常,因此回发..

edit: did a bit of digging based on @rob 's answer, it seems jquery validate is throwing an exception, hence the postback..

这是jquery.validate 1.10

this is jquery.validate 1.10

推荐答案

这对我来说真是一个措辞不好的问题部分我认为。

This was really a poorly worded question on my part I think.

基本上问题根本不是 novalidation 属性(如@robasta所说)。

Essentially the issue was not the novalidation attribute at all (as @robasta said).

我无法得到jQuery 1.9和jQuery.Validate 1.10来玩得很好。切换回jQuery 1.83直接修复它,所有工作正如我所料。

I couldn't get jQuery 1.9 and jQuery.Validate 1.10 to play nicely. Switching back to jQuery 1.83 fixed it straight away, all working as I'd expect.

这篇关于为什么&lt; form&gt;被赋予NoValidate属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆