ajax无刷新判断某公司是否包含某业务

前端之家收集整理的这篇文章主要介绍了ajax无刷新判断某公司是否包含某业务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.jsp页面

<form action="market.do?enter=creaditApplyForAdd"   method="post" name="form1">
 	 <table width="100%"  border="0" cellspacing="0" cellpadding="0">
   
  <tr>
  	<td bgcolor="f7f7f7">  	
  		<table width="100%" >  		
  			<tr>
  				<td colspan="4" align="left" width="100%" bgcolor="LightGrey" ><strong>[::第三方信控业务申请详细信息::]</strong></td>
  			</tr>
  			
  			<tr>
  				<td align="right" width="20%" bgcolor="LightGrey" >第三方登陆用户名: </td>
  				<td align="left" width="30%"><input type="text" id="user_name" name="USER_NAME" readonly="readonly"><input Class="input_btn" type="button" value="选择" onclick="openShowModal('../market/market.do?enter=getCreditUserlist',940,400);"><font color="red"> *</font></td>
  				<td align="right" width="20%" bgcolor="LightGrey" >SP名称:</td>
  				<td align="left" width="30%"><input type="text" id="sp_name" name="SP_NAME" readonly="readonly"></td>
  				
  			</tr>
  			
  			
  			<tr>
  				<td align="right" width="20%" bgcolor="LightGrey" >第三方业务号: </td>
  				<td align="left" width="30%"><input type="text" id="pd_id" name="PD_ID" readonly="readonly"><input Class="input_btn" type="button" value="选择" onclick="getlist()"><font color="red"> *</font></td>
  				<td align="right" width="20%" bgcolor="LightGrey" >第三方业务名称:</td>
  				<td align="left" width="30%"><input type="text" id="bus_name" name="BUS_NAME" readonly="readonly"></td>
  				
  			</tr>
  			<tr>
  				<td align="right" width="20%" bgcolor="LightGrey" >产品名称:</td>
  				<td align="left" width="30%"><input type="text" id="proc_name" name="PROC_NAME" readonly="readonly"></td>
  				<td align="right" width="20%" bgcolor="LightGrey" >基本资费:</td>
  				<td align="left" width="30%"><input type="text" id="proc_fee" name="PROC_FEE" readonly="readonly"></td>
  			</tr>
<table align="center" border="0" >
						<tr >
							<td width="61" align="center">
							</td>
							<td align="center" width="84">
							</td>
							<td width="78" align="center">
								<table width="75" border="0" cellspacing="0" cellpadding="0">
									<tr>
										<td width="316" align="left">
										    <input class="input_btn" type="button" value="添  加" style="font-weight: bold" onclick="dosubmit()"/><input class="input_btn" type="button" value="关  闭" style="font-weight: bold" onClick="javascript:window.close();"/>
										</td>
									</tr>
								</table>
							</td>
							<td width="144">
							</td>
							<td width="0">
							</td>
						</tr>
					</table>
				 </td>
      </tr>
	
 </table>
</form>


2.javascript(ajax方法

	function dosubmit() {
		var username = document.getElementById("user_name").value;
		var pdid=document.getElementById("pd_id").value;
		
		var xmlHttp;
			//根据不同浏览器初始化xmlHttp  
			try {
				//IE 6+  
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					//FireFox  
					xmlHttp = new XMLHttpRequest();
				} catch (e) {
					try {
						//IE 5.5+  
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						alert("您的浏览器不支持Ajax!");
					}
				}
			}
			xmlHttp.open("POST", "market.do?enter=check&username=" + username+"&pdid="+pdid,true);

			xmlHttp.send(null);
			xmlHttp.onreadystatechange = function() {
				if (xmlHttp.readyState == 4) {
					var message=xmlHttp.responseText;
					
					if(message=='已存在'){
						alert("已经有此业务");
						return;
					}else{
						var fom = document.forms[0];
						fom.submit();
					}
					
				}
			}
			
		
		
	}

2.java后台方法(这里的msg对应上面jsp页面xmlHttp.responseText的内容


public ActionForward check(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/xml;charset=GB2312");
		response.setHeader("Cache-Control","no-cache");
		String username = request.getParameter("username");
		String pdid=request.getParameter("pdid");
		
		List list = cmng.check(username,pdid);
		String msg="不存在";
		if(list.size()>0){
			msg="已存在";
		}
		
		try {
			
			response.getWriter().print(msg);
			
			response.getWriter().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		return null;
	}
原文链接:https://www.f2er.com/ajax/165677.html

猜你在找的Ajax相关文章