ajaxForm不工作,不返回错误

时间:2014-09-28 14:22:13

标签: javascript jquery ajax file-upload ajaxform

当我想使用jQuery ajaxForm时,我遇到了一个非常奇怪的问题。

我想设置表单并上传文件,并取得进度百分比。

我的ajaxForm功能根本不是火。

这是我的代码,但我无法找出问题所在,因为我没有错误。

function recieve(res,obj)
{
	var frm = res.substr(1,res.length-8);
	$('#'+frm).find('.error').each(function(){ $(this).remove(); });
	if(obj['alert']!=undefined) $(res).html(obj['alert']);
	if(obj['field']!=undefined) {
		for(var i in obj['field'])
		{
			/*$("#"+i).next('.error').remove();*/
			if(obj['field'][i]!='') $("#"+i).after('<div class="error avesome OC OBC">'+obj['field'][i]+'</div>');
		}
	}
	
	
	if(obj['msgbox']!=undefined) alert(obj['msgbox']);
	if(obj['location']!=undefined) document.location = obj['location'];
}

function sendAjax(form,response,loader,progress)
{
	var frm = $(form);
	frm.ajaxForm({
		dataType: 'json',
		data: frm.serialize()+'&_ajax=1',
		
		beforeSend: function(){
			$("input[type='submit']").attr('disabled','disabled');
			$(progress).width('0%').parent('.progress').removeClass('hidd');
		},
		
		uploadProgress: function(event, position, total, percentComplete){
			var pVel = percentComplete + '%';
			$(progress).width(pVel);
		},
		
		complete: function(data){
			recieve(response,unserialize(data));
			$(progress).parent('.progress').addClass('hidd');
			$("input[type='submit']").attr('disabled',null);
		}
	});
	return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="setting_form" action="http://127.0.0.1/marketing/users/setting/" method="post" onsubmit="return sendAjax('#setting_form','#setting_form_result','#setting_form_loader','#setting_form_progress');">
  <input type="hidden" name="_submit" value="1" />
  <input type="hidden" name="data[setting][id]" id="setting_id" value="11" />
  <input type="hidden" name="data[setting][data-token]" id="setting_data-token" value="8022735" />
  <input type="hidden" name="data[setting][token]" id="setting_token" value="90e18fe55fbc38708456606f4b2b3f96" />
  <input type="submit" name="data[setting][submit]" id="setting_submit" value="send" />
  
  <div id="setting_form_progress" class="bar fade"></div>
  <div id="setting_form_result"></div>
</form>

1 个答案:

答案 0 :(得分:0)

我猜你错过了这个图书馆 http://malsup.com/jquery/form/  之后插入代码 jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>