首页 > 代码库 > dwz框架---(2)表单回调函数

dwz框架---(2)表单回调函数

dwz中的表单回调函数大概有以下几种:

/** * 普通ajax表单提交 * @param {Object} form * @param {Object} callback * @param {String} confirmMsg 提示确认信息 */function validateCallback(form, callback, confirmMsg) {	var $form = $(form);	if (!$form.valid()) {		return false;	}		var _submitFn = function(){		$.ajax({			type: form.method || 'POST',			url:$form.attr("action"),			data:$form.serializeArray(),			dataType:"json",			cache: false,			success: callback || DWZ.ajaxDone,			error: DWZ.ajaxError		});	}		if (confirmMsg) {		alertMsg.confirm(confirmMsg, {okCall: _submitFn});	} else {		_submitFn();	}		return false;}
/** * 带文件上传的ajax表单提交 * @param {Object} form * @param {Object} callback */function iframeCallback(form, callback){	var $form = $(form), $iframe = $("#callbackframe");	if(!$form.valid()) {return false;}	if ($iframe.size() == 0) {		$iframe = $("<iframe id='callbackframe' name='callbackframe' src='about:blank' style='display:none'></iframe>").appendTo("body");	}	if(!form.ajax) {		$form.append('<input type="hidden" name="ajax" value="1" />');	}	form.target = "callbackframe";		_iframeResponse($iframe[0], callback || DWZ.ajaxDone);}function _iframeResponse(iframe, callback){	var $iframe = $(iframe), $document = $(document);		$document.trigger("ajaxStart");		$iframe.bind("load", function(event){		$iframe.unbind("load");		$document.trigger("ajaxStop");				if (iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" || // For Safari			iframe.src == "javascript:'<html></html>';") { // For FF, IE			return;		}		var doc = iframe.contentDocument || iframe.document;		// fixing Opera 9.26,10.00		if (doc.readyState && doc.readyState != 'complete') return; 		// fixing Opera 9.64		if (doc.body && doc.body.innerHTML == "false") return;	   		var response;				if (doc.XMLDocument) {			// response is a xml document Internet Explorer property			response = doc.XMLDocument;		} else if (doc.body){			try{				response = $iframe.contents().find("body").text();				response = jQuery.parseJSON(response);			} catch (e){ // response is html document or plain text				response = doc.body.innerHTML;			}		} else {			// response is a xml document			response = doc;		}				callback(response);	});}/** * navTabAjaxDone是DWZ框架中预定义的表单提交回调函数. * 服务器转回navTabId可以把那个navTab标记为reloadFlag=1, 下次切换到那个navTab时会重新载入内容.  * callbackType如果是closeCurrent就会关闭当前tab * 只有callbackType="forward"时需要forwardUrl值 * navTabAjaxDone这个回调函数基本可以通用了,如果还有特殊需要也可以自定义回调函数. * 如果表单提交只提示操作是否成功, 就可以不指定回调函数. 框架会默认调用DWZ.ajaxDone() * <form action="/user.do?method=save" onsubmit="return validateCallback(this, navTabAjaxDone)"> *  * form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作. statusCode=DWZ.statusCode.error表示操作失败, 提示错误原因.  * statusCode=DWZ.statusCode.timeout表示session超时,下次点击时跳转到DWZ.loginUrl * {"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"", "callbackType":"closeCurrent", "rel"."xxxId"} * {"statusCode":"300", "message":"操作失败"} * {"statusCode":"301", "message":"会话超时"} *  */function navTabAjaxDone(json){	DWZ.ajaxDone(json);	if (json.statusCode == DWZ.statusCode.ok){		if (json.navTabId){ //把指定navTab页面标记为需要“重新载入”。注意navTabId不能是当前navTab页面的			navTab.reloadFlag(json.navTabId);		} else { //重新载入当前navTab页面			var $pagerForm = $("#pagerForm", navTab.getCurrentPanel());			var args = $pagerForm.size()>0 ? $pagerForm.serializeArray() : {}			navTabPageBreak(args, json.rel);		}				if ("closeCurrent" == json.callbackType) {			setTimeout(function(){navTab.closeCurrentTab(json.navTabId);}, 100);		} else if ("forward" == json.callbackType) {			navTab.reload(json.forwardUrl);		} else if ("forwardConfirm" == json.callbackType) {			alertMsg.confirm(json.confirmMsg || DWZ.msg("forwardConfirmMsg"), {				okCall: function(){					navTab.reload(json.forwardUrl);				},				cancelCall: function(){					navTab.closeCurrentTab(json.navTabId);				}			});		} else {			navTab.getCurrentPanel().find(":input[initValue]").each(function(){				var initVal = $(this).attr("initValue");				$(this).val(initVal);			});		}	}}/** * dialog上的表单提交回调函数 * 服务器转回navTabId,可以重新载入指定的navTab. statusCode=DWZ.statusCode.ok表示操作成功, 自动关闭当前dialog *  * form提交后返回json数据结构,json格式和navTabAjaxDone一致 */function dialogAjaxDone(json){	DWZ.ajaxDone(json);	if (json.statusCode == DWZ.statusCode.ok){		if (json.navTabId){			navTab.reload(json.forwardUrl, {navTabId: json.navTabId});		} else if (json.rel) {			var $pagerForm = $("#pagerForm", navTab.getCurrentPanel());			var args = $pagerForm.size()>0 ? $pagerForm.serializeArray() : {}			navTabPageBreak(args, json.rel);		}		if ("closeCurrent" == json.callbackType) {			$.pdialog.closeCurrent();		}	}}/** * 处理navTab上的查询, 会重新载入当前navTab * @param {Object} form */function navTabSearch(form, navTabId){	var $form = $(form);	if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;	navTab.reload($form.attr('action'), {data: $form.serializeArray(), navTabId:navTabId});	return false;}/** * 处理dialog弹出层上的查询, 会重新载入当前dialog * @param {Object} form */function dialogSearch(form){	var $form = $(form);	if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;	$.pdialog.reload($form.attr('action'), {data: $form.serializeArray()});	return false;}function dwzSearch(form, targetType){	if (targetType == "dialog") dialogSearch(form);	else navTabSearch(form);	return false;}/** * 处理div上的局部查询, 会重新载入指定div * @param {Object} form */function divSearch(form, rel){	var $form = $(form);	if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;	if (rel) {		var $box = $("#" + rel);		$box.ajaxUrl({			type:"POST", url:$form.attr("action"), data: $form.serializeArray(), callback:function(){				$box.find("[layoutH]").layoutH();			}		});	}	return false;}


使用方法以前两个为例:

form一般用this代替,callback指的是回调函数,可以自定义,在js里面写一个callback(json)方法,回调时会调用,confirmMsg显示提示信息。

以下是我写的iframeCallback(form, callback)带文件上传的例子,可供参考:

<form id="canshuwwwwww" method="post" action="<%=basePath %>/sdsc/job/jobparamcreate" enctype="multipart/form-data" class="pageForm required-validate" onsubmit="return iframeCallback(this,callback);" > <!-- onsubmit="return iframeCallback(this);" return validateCallback(this,ajaxDone2); -->			<table style="line-height:20px;margin-left:20px;">			<!-- input禁止输入 -->				<tr>					<td><label>选择参数:</label>						<input id="softparamid" name="softwareparam.id" value=http://www.mamicode.com/"" type="hidden"/>>


 

function callback(json){	//alert(json.id);	//提交参数的回调函数		//表格添加一行		newRow=document.all.yltable.insertRow(-1); 		 j_1++;		newRow.id=json.id;		newcell=newRow.insertCell(); 		newcell.className="cell";		//newRow.bgColor='#FFFFFF'; 		newcell.align='center'; 		//newcell.innerHTML="";		newcell.innerHTML="第"+j_1+"行";				newcell=newRow.insertCell(); 		//newRow.bgColor='#FFFFFF';		newcell.className="cell";		newcell.align='center'; 		newcell.innerHTML=json.paramname;				newcell=newRow.insertCell() ; 		//newRow.bgColor='#FFFFFF';		newcell.className="cell";		newcell.align='center';				//alert(document.getElementById("aaaaa"))		//newcell.innerHTML='<input name="button3" type="button" onClick="delrow1()" value=http://www.mamicode.com/"删除1"> ';>