日期:2014-05-16  浏览次数:20665 次

JQUERY 应用AJAX返回字符串
//jquery --AJAX返回字符串————jsp/js部分

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Pragma" CONTENT="no-cache">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Expires" CONTENT="-1">
<script type="text/javascript" src="${path}/js/jquery-1.4.2.min.js"></script>
<script language="javascript">
//jquery --AJAX返回字符串
$(function() {
$("#selectName").change(function(){
var selectName = $("selectName").val();
$.post("${path}xxx.do",
{ selectName:selectName},
function(data){
                    var arr =data.split(",");
                    var A1=arr[0];
                    var A2=arr[1];
                    var A3=arr[2];
                    document.getElementById("msg").innerHTML="<font color='red'>"+A1+"</font>";
    document.getElementById("a2name").value=A2;
    document.getElementById("a3name").value=A3;
}
)

});

});
</script>
</head>
<body>
<form action="" name="frm" id="frm">
<table width="270" border="0" align="center" cellpadding="0" cellspacing="5" class="viewtext">
  <tr align="left" valign="top">
    <td align="right" width="200">下拉列表框的chang事件 jquery --AJAX</td>
    <td width="49" align="left">
       <select name="selectName" id="selectName" />
           <option value="">--请选择--</option>
           <option value="1">AA</option>
           <option value="2">BB</option>
           <option value="3">CC</option>
           <option value="4">DD</option>
       </select>
    </td>
<td id="msg"></td>
<td>
   <input type="text" name="a2name" id="a2name" />
   <input type="text" name="a3name" id="a3name" />
</td>
  </tr>
</table>
</form>
</body>
</html>

//struts2 Action部分

public String getAjaxResult(){
request = ServletActionContext.getRequest();
response=ServletActionContext.getResponse();
String selectName=request.getParameter("selectName");

//业务根据参数selectName得到想要的一个或多个字符串
//如果是一个直接返回,如果是多个可以拼接成一个字符串
//返回结果值代码省略 ,在这只返回三个字符串

try {
String resultString = A1+","+A2+","+A3;
ServletActionContext.getResponse().getWriter().print(resultString);

} catch (IOException e) {
e.printStackTrace();
}
return null;
}