I'm using the jquery.form plugin to asynchronously upload documents in an MVC project.
Taking my lead from this previous answer, here's what I have on the page:
<% 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>
My problem is that when the page loads I get the 开发者_运维知识库following javascript error:
Uncaught TypeError: Object # has no method 'ajaxForm'
This error is found on the line containing
$('#uploadForm').ajaxForm(function (result) {
Can anyone tell me why I'm getting this error?
Check that jquery isn't being included in the page twice.
Here is the method that I am using for MVC3, CodeIgniter and Yii. One important this is that your should specify method="post", enctype and 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();
}
};
Is 'uploadForm' the id of your <form>
object? If it is then you may wan to check in something like FireBug to make sure your plug-in is included on your page properly.
The shortcut:
$(function () {
Doesn't work in latest version of jQuery, have to use:
$(document).ready(function () {
check if you have the $
$('#your-form')
and not
('#your-form')
精彩评论