我正在使用jquery.form插件在MVC项目中异步上传文档。
从this之前的回答中取得领导,这是我在页面上的内容:
<% using(Html.BeginForm("Create", "JobFile", FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" })) %>
<% { %>
<%: Html.ValidationSummary() %>
<input type="file" id="fileToUpload" />
<input type="submit" value="Upload file" />
<input type="text" id="RelatedFileName" />
<% } %>
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.form.js"></script>
<script type="text/javascript">
$(function () {
$('#uploadForm').ajaxForm(function (result) {
if (result.errorMessage != '') {
alert(result.errorMessage);
} else {
$('#RelatedFileName').val(result.fileName);
}
});
});
</script>
我的问题是,当页面加载时,我收到以下javascript错误:
未捕获的TypeError:对象#没有方法'ajaxForm'
在包含
的行上发现此错误$('#uploadForm').ajaxForm(function (result) {
有人能告诉我为什么会收到此错误吗?
答案 0 :(得分:4)
检查页面中是否没有包含jquery两次。
答案 1 :(得分:1)
这是我用于MVC3,CodeIgniter和Yii的方法。 一个重要的是你应该指定 method =“post”, enctype 和 encoding =“multipart / form-data”
var submitForm = function(_formId){
var _genMsg = $('#genMsg');
_submitForm = $('form').index($('#'+_formId));
if(subCommonForm(_submitForm, 'main')){
_genMsg.attr('class', 'information-box round');
_genMsg.html('Saving...');
$('#'+_formId).ajaxForm({
dataType: 'json',
success: function (data) {
_genMsg.attr('class', 'information-box round');
data.code == 0
? _genMsg.attr('class', 'confirmation-box round')
: _genMsg.attr('class', 'error-box round');
_genMsg.html(data.message);
_genMsg.fadeIn();
}
});
$('#'+_formId).submit();
}
};
答案 2 :(得分:0)
'uploadForm'是<form>
对象的ID吗?如果是,那么您可能需要检查FireBug之类的内容,以确保您的插件正确包含在您的页面中。
答案 3 :(得分:0)
快捷方式:
$(function () {
在最新版本的jQuery中不起作用,必须使用:
$(document).ready(function () {
答案 4 :(得分:0)
检查您是否有 $
$('#your-form')
而不是
('#your-form')