jquery ajax json 实例(文本框,下拉框操作)

package com.ygw.stu.stuser.ta.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ps.base.CommonError;
import com.ps.pub.DataManager;
import com.ps.pub.ValueObject;
import com.ygw.stu.base.BaseAction;

/**
 * @author:
 * @version:
 * @Create Time:2011-12-01 19:17:35
 * @Description: 业务处理Action
 */
public class StuNumberAction extends ActionSupport {
	
	
	private String res;
	public String getRes() {
		return res;
	}
	public void setRes(String res) {
		this.res = res;
	}
	
	public String execute(){
		StuNumberBean ajaxTextBean=new StuNumberBean();
        PrintWriter out;
		try {
			//解决中文乱码
			ServletActionContext.getResponse().setContentType("text/json;charset=gb2312");
			
			out = ServletActionContext.getResponse().getWriter();
			String classid="";
			classid=ServletActionContext.getRequest().getParameter("classid");
			
			this.res=ajaxTextBean.getOptionStucodeNames(classid);
			System.out.println(res);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
        
		return this.SUCCESS;
	}
	
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="default" namespace="/ajaxtext" extends="json-default">
    	
    	<!-- ajax start /jsp/stu/stuser/teacher/search.jsp  teacher.list json-default-->
		<action name="jj" class="com.ygw.stu.stuser.teacher.AjaxTextAction">
			<result name="success" type="json">
				<param name="root">res</param>
			</result>
		</action>
		<!-- ajax end -->
		
		
		<!-- teachertreeajaxstudent start -->
		<action name="studenttree" class="com.ygw.stu.stuser.teacher.jsontree.AjaxJsonStudentTreeAction">
			<result name="success" type="json">
				<param name="root">res</param>
			</result>
		</action>
		<!-- teachertreeajaxstudent end -->
		<!-- teachertreeajaxparent start -->
		<action name="parenttree" class="com.ygw.stu.stuser.teacher.jsontree.AjaxJsonParentTreeAction">
			<result name="success" type="json">
				<param name="root">res</param>
			</result>
		</action>
		<!-- teachertreeajaxparent end -->
		<!-- teachertreeajaxexpert start -->
		<action name="experttree" class="com.ygw.stu.stuser.teacher.jsontree.AjaxJsonExpertTreeAction">
			<result name="success" type="json">
				<param name="root">res</param>
			</result>
		</action>
		<!-- teachertreeajaxexpert end -->
		
		
		<!-- ScoreLevelAction start -->
		<action name="scorelevel" class="com.ygw.stu.stuser.ta.json.ScoreLevelAction">
			<result name="success" type="json">
				<param name="root">res</param>
			</result>
		</action>
		<!-- ScoreLevelAction end -->
		
		<!-- StuNumberAction start -->
		<action name="stucodename" class="com.ygw.stu.stuser.ta.json.StuNumberAction">
			<result name="success" type="json">
				<param name="root">res</param>
			</result>
		</action>
		<!-- StuNumberAction end -->
    </package>
</struts>

package com.ygw.stu.stuser.ta.json;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ps.pub.ValueObject;
import com.ygw.stu.base.BaseBean;
import com.ygw.stu.stuser.teacher.AjaxJsonTreeBean;

public class StuNumberBean extends BaseBean {
	public StuNumberBean() {
		super();
		setDaoName("com.ygw.stu.stuser.teacher.dao.TeacherDAO");
	}
	
	
	
	/**
     * 根据班级id查询学生stu号,名字的结果集
     * @param 
     * @return 
     */
    public String getOptionStucodeNames(String classid){
    	/* 结果 student_name  stucode  classid
    	 
select b.STUCODE stucodeb,b.STUDENT_NAME,c.stucode,c.id classid from v_stu_student b,
(
    select * from stu_class_ref a 
    where a.id=''
) c 
where b.STUCODE=c.stucode
    	 */
    	StringBuffer buf=new StringBuffer("<option value=''>请选择...</option>");
    	ArrayList res=new ArrayList();
    	String sql="select b.STUCODE stucodeb,b.STUDENT_NAME,c.stucode,c.id classid from v_stu_student b," +
    			"(" +
    			"select * from stu_class_ref a  " +
    			"where a.id='"+classid+"'" +
    			") c " +
    			"where b.STUCODE=c.stucode";
    	try {
			res=this.select(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	if(res.size()>0){
    		for(int i=0;i<res.size();i++){
    			ValueObject vo=(ValueObject)res.get(i);
    			
    			String class_id=(String)vo.getString("classid");
    			String stucode=(String)vo.getString("stucode");
    			String student_name=(String)vo.getString("student_name");
    			
    			String ss=this.getOptions(stucode, student_name);
    			buf.append(ss);
    			//<option value=''>请选择...</option>
    			//"<option value='1111'>nnnn-1111</option>
    			//<option value='2222'>fsfdsf-2222</option>"
    		}
    	}
    	return buf.toString();
    }
    
     public String getOptions(String stucode,String studentname){
    	 String resStr="";
    	 resStr="<option value='"+stucode+"'>"+studentname+"-"+stucode+"</option>";
    	 
    	 return resStr.toString();
    	 
     }

}


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="t" uri="/WEB-INF/templet.tld" %>
<%@ taglib prefix="app" uri="/WEB-INF/app.tld" %>
<app:jsphelper type="js" src="js/cascade.js"/> 

<%String basePath = request.getScheme() + "://"
		+ request.getServerName() + ":" + request.getServerPort()+ ""
		+ request.getContextPath() + "/"; %>
		
<input type="hidden" id=basePath value="<%=basePath %>" />

<table align="center" valign="top" width="98%" border="0">
<%
	Object photo = session.getAttribute("_PHOTO");
	String photoSrc = "../file/photo.do?size=500";
	if(photo==null)
		photoSrc += "&init=true";
%>

<!—采用iframe的方式处理文件上传-->
<iframe src="<%=photoSrc%>" width="100%" height="300" FRAMEBORDER=0>
</iframe>
  
<br>

	<%-- 编辑信息 --%>	
	<tr>
	    <td width="98%">
			<t:editpanel templet="STUSER_WORK_ASSIST" vo="editVo" col="2"/>
		</td>
		
	</tr>

	<%-- 操作信息 --%>	
	<tr>
		<td align="center" valign="top">
		<app:link type="button" aclass="opt_button" name="确 定" onclick="javascript:onAction('SUBMIT')" border="0"></app:link>
        <app:link type="button" aclass="opt_button" name="重 置" onclick="javascript:onAction('RESET')" border="0"></app:link>
		</td>
	</tr>

</table> 


<script type="text/javascript">

$(document).ready(function(){
	$("#selectTest").change(function()
	{
	       
	       $("#vv").val($("#selectTest").val());
	       alert($("#selectTest option[@selected]").text());
	       

	});
	});


var objCascade1 = new CascadeObject("editVo.string(courseId)","editVo.string(lessonId)","change");
objCascade1.childDivName ="DIV_editVo_lessonId";
objCascade1.childSource = "PubSelectImpl:STUJIANG";
objCascade1.sql = "SQL.COURSE_JIANG";
objCascade1.setElementCascade();

var basePath=document.getElementById("basePath").value;

function getLevel(score){
	//url="ccc&score="+score;
	//onAjaxAction(url,"cmd","target");
}
	
	$(document).ready(function(){
		$(":input[name='editVo.string(workScore)']").blur(function(){
			
			
			var score=$(":input[name='editVo.string(workScore)']").val();
			var   type="^[0-9]*[1-9][0-9]*{1}quot;;
		     var   re   =   new   RegExp(type);
		     if(score.match(re)==null)
		      {
		       alert( "请输入整数!");
		      
		     }else{
		    	 
		    	 var urlpath=basePath+"ajaxtext/scorelevel.do?score="+score;
					jQuery.ajax( {
						url:urlpath,
						type:'post',
						async:false,
						//data:'sql='+sql,
						success:function(res){
							if(""!=res){
								///json字符串转化成json对象方法:  
								//alert("hehe\n"+res);
						        var myJSONObject = eval('(' + res + ')'); 
						       // alert("myJSONObject\n"+myJSONObject);
						        var idv=myJSONObject[0];
						        var namev=myJSONObject[1];
						        
						        $(":input[name='editVo.string(sort)']").val(namev);
						        
						        $(":input[name='editVo.string(scorelevel)']").val(idv);
						       // alert($(":input[name='editVo.string(scorelevel)']").val());
							}
						}
					 });
					
		     }
			
			
		
		//$(":input[name='editVo.string(sort)']").val(dd);
		  });
		});
	
///////
	$(document).ready(function(){
		$(":input[name='editVo.string(classId)']").change(function(){
			
			
			var classid=$(":input[name='editVo.string(classId)']").val();
			//var checkText=$("#ss").find("option:selected").text();
            //var checkValue=$("#ss").val();
            
		     var urlpath=basePath+"ajaxtext/stucodename.do?classid="+classid;
				jQuery.ajax( {
					url:urlpath,
					type:'post',
					async:false,
					//data:'sql='+sql,
					success:function(res){
						if(""!=res){
							///json字符串转化成json对象方法:  
							//alert("hehe\n"+res);
					        //var myJSONObject = eval('(' + res + ')'); 
					       // alert("myJSONObject\n"+myJSONObject);
					       // var idv=myJSONObject[0];
					      //  var namev=myJSONObject[1];
					      
					      $(":input[name='editVo.string(stucode)']").empty();
					      $(res).appendTo(":input[name='editVo.string(stucode)']");
					       // alert($(":input[name='editVo.string(scorelevel)']").val());
						}
					}
				 });
			
			
		
		//$(":input[name='editVo.string(sort)']").val(dd);
		  });
		});

</script>



   

你可能感兴趣的:(jquery,Ajax,json,function,String,input)