ajax同步异步的坑


function companyValueULHtml(){
	var faxhtml ='';
	var tel = '';
	faxhtml += '<select style="width:100px;height:30px;" id="companyfaxcountrycode" name="companyfaxcountrycode" function="changeCompanyFaxCountry()">'
	 //獲取國碼
	$.ajax({
		type : 'post',
		url : '<s:url value="/countryCodeAction_getAjaxCountryCodeList.action"/>',
		async:false,///设置同步请求完后在执行
		dataType : 'json',
		error : function(XMLHttpRequest, textStatus, errorThrown) {
				ErrorReminder(XMLHttpRequest); //请求错误提示
			},
		success : function(data) {
			for(var index in data){
					var country = data[index];
					var countryName = country.countryName;
					 countryCode = country.countryCode;
					 countryNameKey = country.countryNameKey;
					//console.log(JSON.stringify(country));
					if (index == 0){ //默認選擇第一個
				   	 	faxhtml  += '<option  value="'+countryCode+'" selected="selected">'+countryName+'</option>';
				   	 	tel  += '<option  value="'+countryCode+'" selected="selected">'+countryName+'</option>';
				   	 	 mobilecountrycode = countryCode;	//驗證移動電話
						 telcountrycode = countryCode;		//驗證固定电话
						 faxcountrycode = countryCode;		//驗證傳真
					} else{
						faxhtml  += '<option  value="'+countryCode+'">'+countryName+'</option>';
						tel += '<option  value="'+countryCode+'">'+countryName+'</option>';
					}
				}
		
			faxhtml +='</select>';
 			var theHtml = '';
			theHtml += '<li class="customercountrycodeli"><span><p>';
			theHtml += '公司傳真:';
			theHtml += '</p></span>';
			theHtml += faxhtml + '<input name="edcustomercompanyfax" id="companyfax"  />';
			theHtml += '</li>';
		
			theHtml += '<li><span><p>';
			theHtml += '公司主頁:';
			theHtml += '</p></span>';
			theHtml += '<input  name="edcustomercompanyweb" id="companyweb" >';
			theHtml += '</li>';
			
			theHtml += '<li><span><p>';
			theHtml += '<s:text name="1446711635441683">註冊編號</s:text>:';
			theHtml += '</p> </span>';
			theHtml += '<input  name="edcustomercompanynumber" id="companynumber" class="companynumber" >';
			theHtml += '</li>';
			
			theHtml += '<li><span style="display:inline;"><p>';
			theHtml += '<s:text name="1446711635521769">註冊時間</s:text>:';
// 			theHtml += '</p></span><div class="date" style="display:inline;position:relative;"><input id="timespan" οnclick="'+"WdatePicker({skin:'whyGreen',el:'edcustomerbeginningdate',dateFmt:'yyyy-MM-dd'})"+'" name="edcustomerbeginningdate" ></input><img style="position: absolute;" src="images/date1.png" οnclick="'+"WdatePicker({skin:'whyGreen',el:'edcustomerbeginningdate',dateFmt:'yyyy-MM-dd'})"+'"/>';
			theHtml += '</p></span><div class="date" style="display:inline;position:relative;"><input id="timespan" disabled="disabled"></input><img style="position: absolute;" src="images/date1.png"   id="edcustomerbeginningdateimg"/>';
			theHtml += '<input type="hidden" name="edcustomerbeginningdate" id="timehide"/>';

			theHtml += '</div></li>';
			if(tax_rate == 1){//税率GST
				theHtml += '<li><span><p>';
				theHtml += '<s:text name="1469600830924705">GST編號</s:text>:';
				theHtml += '</p> </span>';
				theHtml += '<input  name="edcustomergstno" id="gstno" >';
				theHtml += '</li>'; //先隐藏掉  之后在改
			}
			//$("#ctelcountrycode").html(tel);
			//$("#ctelcountrycode").append(tel);
			//$("#ctelcountrycode").styleSelect({styleClass:"selectDark"});//樣式
			
			$("#changeValueUL").html(theHtml);
			$.parser.parse($("#changeValueUL"));
			$("#companyfaxcountrycode").styleSelect({styleClass:"selectDark"});//樣式
			bindingcompanyCInfoVer();	//綁定客戶 在去驗證
			//companyValueValidation_on();//开启驗證
		}
	});	
}
以上是动态加载html方法
//构建 公司客户信息框
function companyCustomerInfo(customerdata){
	//debugger;
	companyValueULHtml();//构建html
	bindingcompanyCInfoVer();
	//设置信息验证}
在此方法调用
// 根据customer 类型构造信息表单
function customerTypeInfochange(type_flag,customerdata){
		if(theEditFlag == 'addc'){
			$('#edaccountbirthdayimg').css("right","12px");
			$('#edaccountbirthdayimg').css("top","8px");
			edaccountbirthdayimgdatadiv.css("left","");
		}
		if(type_flag == 1){//ycw:大客户 and 公司客户
			$('#customereditmenudiv').css("height",getHeight(660,100));
			$('#changeValueUL').show();
			 document.getElementById("customerNameP").innerHTML = '<s:text name="1446711635481558">公司名稱</s:text>:';
			 //客户姓名提示相关
			$("#customerprompt").css("display","none");
			if(theEditFlag == 'addc'){
				$('.accountNameLi').show();								
				$('#firstname').verification('enableValidation'); //启动输入框验证 
				$('#secondname').verification('enableValidation'); //启动输入框验证 
				$('#accountfaxLi').hide();
				/* var theLI = document.getElementById("accountmobleTelLi");
				theLI.innerHTML =accountMoblieliHtml();
				$.parser.parse(theLI); */
				//accountMoblieliHtml();
				$('#accountmobleTelLi').show();
				creatajaxDateValidate('mobile','mobile','mobile',true,ajaxDateValidateUrl,"accountmobile");
				$('#mobile').verification('enableValidation'); //启动输入框验证
				$('#mobile').verification('setajaxDefultval',''); //设置当前值
				
				//$("#mobilecountrycode").styleSelect({styleClass:"selectDark"});
			}
			companyCustomerInfo(customerdata);
		}}

然后通过select标签选择类型的时候进行出发

//客户类型选择变更执行方法   改变对应的客户类型动态属性ul
function customerTypeSelChange(option,customerdata){
	$.ajax({
		type : 'post',
		url:'<s:url value="customerType_ajaxGetCustomerType.action"/>?Typeid='+option,
		dataType : 'json',
		success : function(data){
			var theUl = document.getElementById("changeValueUL");
			var childs = theUl.childNodes;
			for ( var i = childs.length - 1; i >= 0; i--) {
				theUl.removeChild(childs[i]);
			}
			customertypeflag = data.customertypeflag;
			/************私人用户,学生******去掉电话**************2016-12-30*******************************************************************/
			if(data.customertypeflag == 3 || data.customertypeflag == 2){
				$('#companytel').verification('disableValidation'); //停止输入框验证 
				$('#companyfax').verification('disableValidation'); //停止输入框验证 
				//$('#companyfax').removeClass("verification-invalid");
				$('#companytel').val("");
				$('#telli').css({"display":"none"});
			}else{
				$('#companytel').verification('enableValidation'); //启动输入框验证 
				$('#companyfax').verification('enableValidation'); //启动输入框验证 
				//$('#companyfax').removeClass("verification-invalid");
				//$('#companyfax').addClass("verification-invalid");
				$('#telli').css({"display":"block"});
			}
			/*********************************************************2016-12-30***********************************************************/
			customerTypeInfochange(data.customertypeflag,customerdata);
		}
	});
}
主要原因是  当页面加载时第一次
 companyValueULHtml()
异步加载时 没加载完返回结果接跳过了~ 查看了一下~居然循环2次结果,当你设置debugger; 或者alert 时 就会出现回填
companyCustomerInfo(customerdata)
此方法才真正获取到
companyValueULHtml()
异步的html元素~  所以 需在
companyValueULHtml()
方法里面ajax请求设置同步请求~ 意思是ajax同步完后再去执行以下操作。


发布了33 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/sinat_26987533/article/details/54601049