// 取值
// alert($("input[name='username']").val());
// alert($("input[name='password']").val());
// alert($("input[name='sex']:checked").val());
// alert($("input[name='sex']:checked").prev('label').text());
$("input[name=hobby]:checked").each(function(){
alert($(this).val());
})
// alert($("select[name='county'] option:selected").val());
// 或
// alert($("#county option:selected").val());
// 或
// alert($("#county option:first").val());
// 或
// alert($("#county option:eq(1)").val());
// 设置value
// alert($("#county").attr("value",'china'));
$("#county option").each(function(i){
// alert(i);
// alert($(this).text());
})
//或
// alert($("#county option[value='china']").text())
$.ajax({
url:"<%=context%>/costdept.do?method=saveUser",
type:'post',
dataType:'text',
contentType: "application/x-www-form-urlencoded; charset=utf-8",
async:false,
data:$("#form").serialize(),
success:function(ary){
var data1 = $.parseJSON(ary);
alert(data1.arr[0]);
},
error:function(data, status, e){
alert(e);
}
});
// 1、返回text
//response.getWriter().print("OK");
// 2、返回json
// System.out.println(request.getParameter("username"));
// JSONArray array =new JSONArray();
// JSONObject json = new JSONObject();
// json.put("china", "中国");
// json.put("english","韩国");
// array.add(json);
// json = new JSONObject();
// json.put("green", "绿色");
// json.put("pink","粉色");
// array.add(json);
// response.getWriter().print(array);
// 3、Map List bean 数组 与json转化 参考JSONObject.fromObject
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
//map.put("func", new String[]{"\\'function(i){ return this.arr[i]; }\\'"});
JSONObject json = JSONObject.fromObject(map);
String jsonStr=json.toString();
// 设置编码
response.setCharacterEncoding("UTF-8");
response.getWriter().print(jsonStr);
return null;