我正在制作一个MVC 4应用程序。我试图使用jQuery验证应用一些客户端验证。我一直收到"未捕获的类型错误:对象[对象对象]没有方法'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。试着把标签拿出来,一次一个,把它们放在不同的顺序,把它们放在主布局页面,等等……真的被这个难住了。当然,有时它是难以置信的简单。
figure ..你有两个相同的脚本加载在页面..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>
..........