jQuery 选择下拉验证客户端站点不起作用 [英] jQuery Chosen Dropdown validation client site doesn't work

查看:30
本文介绍了jQuery 选择下拉验证客户端站点不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型类包含必需的查找值,它是基于查找的记录:

[必填][显示名称(业务单位")]公共字符串值 { 获取;放;}[必需]//不在客户端工作?[显示名称(业务组")]公共 int id_businessgroup { 获取;放;}

查看:

 

@Html.LabelFor(model => model.value)

<div class="editor-field">@Html.EditorFor(model => model.value)@Html.ValidationMessageFor(model => model.value)

<div class="editor-label">@Html.LabelFor(x=>x.BusinessGroup.value)

<div class="editor-field">@Html.DropDownListFor(x => x.id_businessgroup, new SelectList(ViewBag.BusinessGroups,"id","value"),"请从列表中选择组...")@Html.ValidationMessageFor(x => x.id_businessgroup)

@section 脚本{@Html.Partial("ScriptUseChosen")}

控制器:

public ActionResult Create(){ViewBag.BusinessGroups = DB.BusinessGroups.Where(x => x.is_active).OrderBy(x => x.value).ToList();返回视图();}

Web.config:

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

BundleConfig.cs:

 bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.validate*","~/Scripts/jquery.unobtrusive*"));

value 的验证在客户端完美运行,但它不适用于选择的下拉列表.可能是什么原因?

解决方案

jQuery Chosen 通过隐藏原始选择来更新 html (style="Display:none").默认情况下 jquery.validate (1.9.0) 忽略隐藏元素.您可以使用

覆盖默认值

$.validator.setDefaults({忽略: []});

$('form').validate({忽略: []});

My model class contains of required lookup value which is a lookup-based record:

[Required]
[DisplayName("Business Unit")]
public string value { get; set; }

[Required] //not working on client side?
[DisplayName("Business Group")]
public int id_businessgroup { get; set; }

View:

    <div class="editor-label">
        @Html.LabelFor(model => model.value)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.value)
        @Html.ValidationMessageFor(model => model.value)
    </div>

    <div class="editor-label">
        @Html.LabelFor(x=>x.BusinessGroup.value)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.id_businessgroup, new SelectList(ViewBag.BusinessGroups,"id","value"),"Please select group from list...")
        @Html.ValidationMessageFor(x => x.id_businessgroup)
    </div>

@section scripts{
  @Html.Partial("ScriptUseChosen")
}

Controller:

public ActionResult Create()
    {
        ViewBag.BusinessGroups = DB.BusinessGroups.Where(x => x.is_active).OrderBy(x => x.value).ToList();
        return View();
    }

Web.config:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

BundleConfig.cs:

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.validate*",
            "~/Scripts/jquery.unobtrusive*"));

Validation for value is working perfectly on client side, but it's not working on the Chosen's dropdown. What could be the reason?

解决方案

jQuery Chosen updates the html by making the original select hidden (style="Display:none"). By default jquery.validate (1.9.0) ignores hidden elements. You can override the default using

$.validator.setDefaults({ 
  ignore: []
});

or

$('form').validate({
    ignore: []
});

这篇关于jQuery 选择下拉验证客户端站点不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆