我正在制作一个MVC 4应用程序。我正在尝试使用jQuery验证来应用一些客户端验证。我一直收到“Uncaught Type Error:Object [Object object]没有方法'validate'”。我尝试过使用url链接而不是项目中的脚本。我检查了脚本,它们确实存在。我已将脚本拖放到文件中以确保正确指定路径。我已经尝试了很多其他的东西,但是无法弄清楚为什么我会收到这个错误。
这是我呈现的页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Create</title>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.5.3.js"></script>
</head>
<body>
<h2>Create Question For d</h2>
<script src="/Scripts/jquery-1.7.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//form validation
$('#myForm').validate();
$(':input').rules("add", {
required: true
});
//submit form using jquery
//$('#myForm').submit(function (event) {
// //url-encode the form data
// var formData = $(this).serialize();
// //post the data to the controller
// $.post(
// 'AddQuestion',
// formData
// );
// event.preventDefault();
// handleSuccess();
//});
//$(':input').not(':button, :submit, :reset, :hidden').val('');
//$(':checkbox').attr('checked', false);
$("input:checkbox").click(function () {
if ($(this).is(":checked") === true) {
$('input:checkbox').attr("checked", false);
$(this).attr("checked", true);
} else {
$(this).is(":checked", false);
}
});
//clears form on page load (called from submit function)
function handleSuccess() {
$(':input').not(':button, :submit, :reset, :hidden').val('');
$(':checkbox').attr('checked', false);
}
});
</script>
<form action="/Test/AddQuestion" id="myForm" method="post"> <fieldset>
<legend>Question</legend>
<!-- input box for question -->
<input type="text" name="question.query" />
<h3>Answers</h3>
<input type="text" name="answer[0].option" /> <input type="checkbox" name="answer[0].isCorrect" value="true" /> <br />
<input type="text" name="answer[1].option" /> <input type="checkbox" name="answer[1].isCorrect" value="true"/> <br />
<input type="text" name="answer[2].option" /> <input type="checkbox" name="answer[2].isCorrect" value="true"/> <br />
<input type="text" name="answer[3].option" /> <input type="checkbox" name="answer[3].isCorrect" value="true"/> <br />
<!-- Passes testId & testName to AddQuestion Controller Action -->
<div>
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="test_Id" name="test.Id" type="hidden" value="222" />
<input id="test_name" name="test.name" type="hidden" value="d" />
</div>
<p>
<input type="submit" value="Add Question to Test" />
</p>
</fieldset>
</form>
<div>
<a href="/Test">Back to List</a>
</div>
<script src="/Scripts/jquery-1.7.1.js"></script>
</body>
</html>
还没有找到解决方案。尝试过使用托管链接标记src。尝试将标签分开,一次一个,将它们放在不同的顺序中,将它们放在主布局页面等等......真的难倒了。当然,它有时候简直令人难以置信。
答案 0 :(得分:2)
想一想..你在页面中加载了两个相同的脚本.. jquery
和jquery.min
..这根本不是必需的..
只需加载min.js即可...并且总是建议您在<head>
上加载所有js文件而不是<body>
...
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Create</title>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.5.3.js"></script>
<script src="/Scripts/jquery-1.7.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
</head>
<body>
..........