jQuery学习手册

发表时间: 2019-01-03 17:30

Jike五点半,每天五点半进步一点点

一、map与each的使用

<html><head><meta charset="utf-8"> <title>jquerydemo</title> <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ $("input").each(function(index, item){ alert($(item).val()); }); $("input").map(function(){ alert($(this).val()); });});</script></head><body><input type="text" value="1"/><input type="text" value="2"/> <input type="text" value="3"/> <input type="text" value="4"/></body></html>

二、ajax

1)jQuery前端代码

 $(function () { $.ajax({ type: "GET", url: "some", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg.name ); alert( "Data Saved: " + msg.location ); } }); }); $(function () { $.ajax({ type: "POST", url: "some", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg.name ); alert( "Data Saved: " + msg.location ); } }); }); 

2)springmvc后端controller层

 @RequestMapping("/some") @ResponseBody public Map<String, Object> getSome(String name, String location) throws IOException { System.out.println("GET"); Map<String,Object> map = new HashMap<String,Object>(); map.put("name",name); map.put("location",location); return map; } @RequestMapping(value = "/some",method = RequestMethod.POST) @ResponseBody public Map<String, Object> postSome(String name, String location) throws IOException { System.out.println("POST"); Map<String,Object> map = new HashMap<String,Object>(); map.put("name",name); map.put("location",location); return map; }

三、JSON.parse()和JSON.stringify()区别

之前有个小伙伴私信问我如何将json字符串转化为json对象,后端的我已经写过了,今天用jquery写了前端的

JSON.parse()【从一个字符串中解析出json对象】例子://定义一个字符串var data='{"name":"goatling"}'//解析对象​ 就是将json字符串转换为对象​JSON.parse(data)结果是:​name:"goatling"JSON.stringify()【从一个对象中解析出字符串】 说白了就是将json对象转换为字符串var data={name:'goatling'}JSON.stringify(data)结果是:'{"name":"goatling"}'