Anonymous
Ошибка: Uncaught TypeError: $(...).valid не является функцией при использовании ('#form').valid()
Сообщение
Anonymous » 30 ноя 2025, 13:01
Вот моя страница, на которой я хочу проверить корректность формы с помощью функции jquery. Код следующий:
Код: Выделить всё
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "addNominationForm"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.Label("Nomination Category", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" })
@Html.DropDownListFor(model => model.AwardId, @ViewBag.Awards as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Award", style = "width:25%" })
@Html.Label("Resource From", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" })
@Html.RadioButtonFor(model => model.SelectResourcesBy, "Project", new { htmlAttributes = new { @class = "form-control" }, id = "ProjectRadioButton" })Project
@Html.RadioButtonFor(model => model.SelectResourcesBy, "Department", new { htmlAttributes = new { @class = "form-control" }, id = "DepartmentRadioButton" })Department
@Html.Label("Project", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" })
@Html.DropDownListFor(model => model.ProjectID, @ViewBag.ProjectsUnderCurrentUser as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "SelectedProject", style = "width:25%" })
@Html.Label("Department", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" })
@Html.DropDownListFor(model => model.DepartmentId, @ViewBag.DepartmentsUnderCurrentUser as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "SelectedDepartment", style = "width:25%" })
@Html.Label("Resource Name", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" })
@Html.DropDownListFor(model => model.ResourceId, @ViewBag.Resources as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Resources", style = "width:25%" })
@Html.Label("Is PLC ?", htmlAttributes: new { @class = "control-label col-md-3", style = "text-align: left" })
@Html.CheckBoxFor(model => model.IsPLC, new { htmlAttributes = new { @class = "form-control" }, id = "IsPlcCheckbox" }) Yes
@Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
@Html.Label("Comment", htmlAttributes: new { @class = "control-label col-md-2 requiredfeild", style = "text-align: left" })
@Html.TextAreaFor(model => model.MainComment, new { htmlAttributes = new { @class = "form-control" }, id = "managerComment", style = "min-width: 80%", rows = "7", placeholder = "If candidate is selected as winner, this text will be shared across the company" })
}
function checkCriteriaComments() {
var comments = 0;
if($("#addNominationForm").valid())
{
alert("valid form");
}
$(".criteriaComment").children("textarea").each(function (data) {
if ($(this).val().trim())
comments++;
});
if (!$("#Award").val()) {
$("#Award").tooltip({ title: "Select Award.", placement: 'right' }).tooltip('show');
return false;
}
else if (comments < 1) {
$(".criteriaComment").children("textarea").first().tooltip({ title: "Enter at least one criteria comment.", placement: 'right' }).tooltip('show');
return false;
}
else if (!$("#managerComment").val()) {
$("#managerComment").tooltip({ title: "Enter main comment.", placement: 'right' }).tooltip('show');
return false;
}
else {
swal({
title: "Add Nomination",
text: "Do you want to nominate this candidate?",
type: "info",
showCancelButton: true,
confirmButtonColor: "#88a364",
confirmButtonText: "Yes!",
cancelButtonText: "No!",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
if (isConfirm) {
debugger;
var model = $('#addNominationForm').serialize()
$.ajax({
type: 'post',
url: '@Url.Action("AddNomination", "Nomination")',
data: JSON.stringify({ model: model,submit:"Submit" }),
contentType: "application/json; charset=utf-8",
success: function (data) {
swal({
title: "Nominated Successfully",
showCancelButton: false,
confirmButtonColor: "#88a364",
confirmButtonText: "Ok"
},
function () {
if (isConfirm) {
window.location.href = '@Url.Action("Dashboard", "Dashboard")';
}
});
}
});
} else {
swal("Cancelled", "Nomination Cancelled");
}
});
return true;
}
}
}
Это моя модель:
Код: Выделить всё
public class NominationViewModel
{
[Required(ErrorMessage = "Select Award")]
public int AwardId { get; set; }
public int NominationId { get; set; }
public int ManagerId { get; set; }
[Required(ErrorMessage = "Select Resource")]
public int ResourceId { get; set; }
[Required(ErrorMessage = "Select Project")]
public int? ProjectID { get; set; }
[Required(ErrorMessage = "Select Department")]
public int? DepartmentId { get; set; }
public bool IsPLC { get; set; }
public bool? IsSubmitted { get; set; }
public string MainComment { get; set; }
[Required(ErrorMessage = "Select Project or Department")]
public string SelectResourcesBy { get; set; } //from project or from department
[Required(ErrorMessage = "Please provide comment against criteria")]
public IList Comments { get; set; }
public NominationViewModel()
{
Comments = new List();
}
}
Здесь, когда я отлаживаю код сценария в браузере, он выдает «Uncaught TypeError: $(...).valid is not a function» эту ошибку в функции valid(). Как можно решить эту проблему? заранее спасибо...
Подробнее здесь:
https://stackoverflow.com/questions/377 ... ing-form-v
1764496910
Anonymous
Вот моя страница, на которой я хочу проверить корректность формы с помощью функции jquery. Код следующий: [code] @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "addNominationForm"})) { @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.Label("Nomination Category", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" }) @Html.DropDownListFor(model => model.AwardId, @ViewBag.Awards as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Award", style = "width:25%" }) @Html.Label("Resource From", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" }) @Html.RadioButtonFor(model => model.SelectResourcesBy, "Project", new { htmlAttributes = new { @class = "form-control" }, id = "ProjectRadioButton" })Project @Html.RadioButtonFor(model => model.SelectResourcesBy, "Department", new { htmlAttributes = new { @class = "form-control" }, id = "DepartmentRadioButton" })Department @Html.Label("Project", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" }) @Html.DropDownListFor(model => model.ProjectID, @ViewBag.ProjectsUnderCurrentUser as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "SelectedProject", style = "width:25%" }) @Html.Label("Department", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" }) @Html.DropDownListFor(model => model.DepartmentId, @ViewBag.DepartmentsUnderCurrentUser as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "SelectedDepartment", style = "width:25%" }) @Html.Label("Resource Name", htmlAttributes: new { @class = "control-label col-md-3 requiredfeild", style = "text-align: left" }) @Html.DropDownListFor(model => model.ResourceId, @ViewBag.Resources as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Resources", style = "width:25%" }) @Html.Label("Is PLC ?", htmlAttributes: new { @class = "control-label col-md-3", style = "text-align: left" }) @Html.CheckBoxFor(model => model.IsPLC, new { htmlAttributes = new { @class = "form-control" }, id = "IsPlcCheckbox" }) Yes @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" }) @Html.Label("Comment", htmlAttributes: new { @class = "control-label col-md-2 requiredfeild", style = "text-align: left" }) @Html.TextAreaFor(model => model.MainComment, new { htmlAttributes = new { @class = "form-control" }, id = "managerComment", style = "min-width: 80%", rows = "7", placeholder = "If candidate is selected as winner, this text will be shared across the company" }) } function checkCriteriaComments() { var comments = 0; if($("#addNominationForm").valid()) { alert("valid form"); } $(".criteriaComment").children("textarea").each(function (data) { if ($(this).val().trim()) comments++; }); if (!$("#Award").val()) { $("#Award").tooltip({ title: "Select Award.", placement: 'right' }).tooltip('show'); return false; } else if (comments < 1) { $(".criteriaComment").children("textarea").first().tooltip({ title: "Enter at least one criteria comment.", placement: 'right' }).tooltip('show'); return false; } else if (!$("#managerComment").val()) { $("#managerComment").tooltip({ title: "Enter main comment.", placement: 'right' }).tooltip('show'); return false; } else { swal({ title: "Add Nomination", text: "Do you want to nominate this candidate?", type: "info", showCancelButton: true, confirmButtonColor: "#88a364", confirmButtonText: "Yes!", cancelButtonText: "No!", closeOnConfirm: false, closeOnCancel: false }, function (isConfirm) { if (isConfirm) { debugger; var model = $('#addNominationForm').serialize() $.ajax({ type: 'post', url: '@Url.Action("AddNomination", "Nomination")', data: JSON.stringify({ model: model,submit:"Submit" }), contentType: "application/json; charset=utf-8", success: function (data) { swal({ title: "Nominated Successfully", showCancelButton: false, confirmButtonColor: "#88a364", confirmButtonText: "Ok" }, function () { if (isConfirm) { window.location.href = '@Url.Action("Dashboard", "Dashboard")'; } }); } }); } else { swal("Cancelled", "Nomination Cancelled"); } }); return true; } } } [/code] Это моя модель: [code] public class NominationViewModel { [Required(ErrorMessage = "Select Award")] public int AwardId { get; set; } public int NominationId { get; set; } public int ManagerId { get; set; } [Required(ErrorMessage = "Select Resource")] public int ResourceId { get; set; } [Required(ErrorMessage = "Select Project")] public int? ProjectID { get; set; } [Required(ErrorMessage = "Select Department")] public int? DepartmentId { get; set; } public bool IsPLC { get; set; } public bool? IsSubmitted { get; set; } public string MainComment { get; set; } [Required(ErrorMessage = "Select Project or Department")] public string SelectResourcesBy { get; set; } //from project or from department [Required(ErrorMessage = "Please provide comment against criteria")] public IList Comments { get; set; } public NominationViewModel() { Comments = new List(); } } [/code] Здесь, когда я отлаживаю код сценария в браузере, он выдает «Uncaught TypeError: $(...).valid is not a function» эту ошибку в функции valid(). Как можно решить эту проблему? заранее спасибо... Подробнее здесь: [url]https://stackoverflow.com/questions/37701545/erroruncaught-typeerror-valid-is-not-a-function-while-using-form-v[/url]