- 
							
ajax,jquery和js的一些事
普通类 - 
							
- 支持
 - 批判
 - 提问
 - 解释
 - 补充
 - 删除
 
 - 
							
- 
													关于Ajax传值
 
var type,userId,sourceId,authority;
function showGroupName(obj,userId2,sourceId2,type2,authority2){
type = type2;
userId = userId2;
sourceId=sourceId2;
authority=authority2;
//alert("userId="+userId);
//alert("friendId="+friendId);
$('input[id^=groupNameId_]').removeAttr("checked");
$.ajax({
url:'/do/resourcestorage?action=getNodeNamesByAjax&sourceId='+sourceId+'&userId='+userId+'&type='+type, //方法和参数
type:'GET',
dataType : 'json',
timeout: 50000,
success : function( data ){
if( data.status == "success" ){
window.location.reload();
}else{
alert("getNodeNamesByAjaxfailed");
}
}
});
}- 
													关于JSONObject和JSONArray
 
Service中从数据库读取List返回action后做处理,封装到object里面,然后包一层JSONArray。再放到JSONObject里面传回页面
action中方法
/**
* 【通过Ajax方式返回指定用户的某个资源所在的所有分类】
* @author tangyao
* @param mapping
* @param form
* @param req
* @param res
* @return
* @throws HepServiceException
* @throws JSONException
* @throws IOException
*/
public ActionForward getNodeNamesByAjax(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws HepServiceException, JSONException, IOException {
Long userId=Long.parseLong(req.getParameter("userId"));
String type = ""+req.getParameter("type");
Long sourceId=Long.parseLong(req.getParameter("sourceId"));
res.setContentType("text/html;charset=UTF-8");
res.setHeader("Cache-Control", "no-cache");
PrintWriter out = res.getWriter();
JSONObject allInfo = new JSONObject(); //传回jsp页面的object
JSONObject arrayObject= new JSONObject(); //将含有具体信息的JSONArray包装起来
JSONArray jsonArray = new JSONArray(); //含具体信息的Array
List rsList = ResourceStorageService.getInstance().GetByTypeAndSourceId(userId, sourceId,type); //数据库读取列表
try{
Long rsId=null;
int count=0;
ResourceStorage rs=null;
if(rsList!=null&&rsList.size()>0)
{
count=rsList.size();
for(int i=0;i<count;i++)
{
rs = (ResourceStorage)rsList.get(i);
JSONObject jasonTemp= new JSONObject(); //单条的信息处理后放到这个object中,然后每一个都存入array数组中
jasonTemp.put("id", rs.getId());
jasonTemp.put("nodeId", rs.getNodeId());
jsonArray.put(jasonTemp);
}
}
allInfo.put("count", count);
arrayObject.put("info", jsonArray);
allInfo.put("detail", arrayObject);
allInfo.put("status", "success"); //层层封装,最外层可放一些描述性信息,便于提取
}catch (Exception e) {
e.printStackTrace();
allInfo.put("status", "failed");
} finally {
out.print(allInfo.toString());
out.flush();
out.close();
}
return null;
}对应的jsp页面
$.ajax({
url:'/do/resourcestorage?action=getNodeNamesByAjax&sourceId='+sourceId+'&userId='+userId+'&type='+type,
type:'GET',
dataType : 'json',
timeout: 50000,
success : function( data ){
if( data.status == "success" ){ //对传回的数据的处理,不用看内部细节
for(var k=0;k<data.count;k++){
var groupIdTemp="groupNameId_"+data.detail.info[k].nodeId;
//alert(groupIdTemp);
$("#"+groupIdTemp).attr("checked",true);//打勾
}
}else{
alert("getNodeNamesByAjaxfailed");
}
}
});- 
													jQuery命令
 
$('input[id^=groupNameId_]').removeAttr("checked"); //取消id前半部分是“groupNameId_”的复选框中的√
$("#groupNameId_101").attr("checked",true); //给id=groupNameId_101的复选框前面打勾
if($("#groupNameId_101").attr('checked')==undefined) //判断是否已经打勾
参考source:jQuery控制组件的 http://yilong511.iteye.com/blog/542094 2012-7-14
 - 
													
 - 
							
- 标签:
 - jsonobject
 - 常用命令
 - jsonarray
 - param
 - 39
 - success
 - id
 - type
 - throws
 - allinfo.put
 - userid
 - jquery
 
 - 
				
				
				
 
			
								
								
						
学习元评论 (0条)
聪明如你,不妨在这 发表你的看法与心得 ~