gpt4 book ai didi

jquery - 如何使用 selected.js 在 MVC 中实现客户端验证

转载 作者:行者123 更新时间:2023-12-01 01:23:01 25 4
gpt4 key购买 nike

我使用 chosen.js 进行多项选择,通过 asp.net mvc3 实现。客户端验证无法使用此多选控件。

我实现的方式是,使用 Html.Dropdownlist 帮助器,一旦从 JavaScript 加载 dom $("#dropdown1").chosen()

它正在用 div 替换 Select。

下面是代码

回调代码

function (data) {
$("#divnewdata").html(data);

$("#ddlcode1").chosen({
disable_search_threshold: 10,
no_results_text: "Oops, nothing found!",
disable_search: false,
});

$("#ddlCode1_chosen").find('.chosen-choices').removeClass('form-control').addClass('form-control');

$("#ddlcode2").chosen({
disable_search_threshold: 10,
no_results_text: "Oops, nothing found!",
disable_search: false,
});

$("#ddlcode2_chosen").find('.chosen-choices').removeClass('form-control').addClass('form-control');

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

}

从布局页面加载java脚本

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script>

$('form').validate({
ignore: []
});
</script>
<script src="~/Scripts/Common/Common.js"></script>
</head>

正在尝试下载部分 View

<script>
$(document).ready(function () {
$.validator.unobtrusive.parse("#formAddcodes");

$.validator.setDefaults({
ignore: []
});
});
</script>

<link href="~/Content/chosen.css" rel="stylesheet" />
<script src="~/Scripts/chosen.jquery.min.js"></script>
<script src="~/Scripts/Codes/Codes.js"></script>


@using (Html.BeginForm("AddCodes", "Codes", FormMethod.Post, new { id = "formAddcodes" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.code1, htmlAttributes: new { @class = "col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.code1, Model.Code1List as SelectList, new { @class = "form-control", id = "ddlcode1", multiple = "multiple" })
@Html.ValidationMessageFor(model => model.code1, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.code2, htmlAttributes: new { @class = "col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.code2, Model.code2List as SelectList, new { @class = "form-control", id = "ddlcode2", multiple = "multiple" })
@Html.ValidationMessageFor(model => model.code2, "", new { @class = "text-danger" })
</div>
</div>


<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Add" class="btn btn-primary" />
</div>
</div>
</div>
}

最佳答案

chosen通过制作原始 <select> 来更新 html默认情况下会忽略隐藏元素和隐藏元素。您可以使用以下脚本修改此行为

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

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

关于jquery - 如何使用 selected.js 在 MVC 中实现客户端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30285222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com