當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Jquery 点击当前的标签对象获取值 与JS整理
生活随笔
收集整理的這篇文章主要介紹了
Jquery 点击当前的标签对象获取值 与JS整理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
太原 2019-6-24 工作經歷
文章總結
在jquery中
div元素被點擊時,使用 $(this)
獲取當前div的id 使用 $(this).attr(“id”);
獲取當前div的name 使用 $(this).attr(“name”);
獲取當前div便簽對中的值 使用 $(this).text();
在Freemark中 Js的變量與jquery結合使用的話
#要加引號 用 +號與變量連接
$("#"+memberId).removeClass(‘collected’).addClass(‘collectio’).html(‘已收藏’);
//2020-5-23更新
批量獲取select多選框
var dateValue= $(“select[name=‘interest’]”).get(i).value
項目中用的時 Freemark渲染
Html中
JS中
$(function(){//添加收藏$(".collected").click(function(){var value = $(this).text();var memberId = $(this).attr("id");if(value == "收藏"){$.ajax({type : "get",dataType : "json",url : "${base}/collect/inCollect",contentType:"application/x-www-form-urlencoded",data:{"memberId":memberId },success:function (data) {if(data.code == 1){$("#"+memberId).removeClass('collected').addClass('collectio').html('已收藏');}else{layer.msg(data.msg, {icon: 2, time: 800});}},//響應成功后的回調函數});}else{$.ajax({type : "get",dataType : "json",url : "${base}/collect/outCollect",contentType:"application/x-www-form-urlencoded",data:{"memberId":memberId },success:function (data) {if(data.code == 1){$("#"+memberId).removeClass('collectio').addClass('collected').html('收藏');}else{layer.msg(data.msg, {icon: 2, time: 800});}},//響應成功后的回調函數});}});});后臺
@Controller @RequestMapping("/collect") public class BookCollectController {@RequestMapping("/inCollect")public void inCollect(Model model, @RequestParam(required = false, value = "memberId", defaultValue = "") String memberId,HttpServletRequest request, HttpServletResponse response) throws IOException {Map<String,Object> result = Maps.newHashMap(); //定義一個HashMap集合 存儲數據 返回前臺/---查詢數據庫的驗證跳過----/result.put("code", 1);result.put("msg", "收藏成功");printJSON(getStringOfObj(result), response); } //封裝的方法。 protected void printJSON(Object o, HttpServletResponse response) {PrintWriter out=null;try {response.setContentType("application/json;charset=UTF-8");out = response.getWriter();out.print(o);out.flush();} catch (IOException ex) {logger.error(ex.getMessage());}catch (Exception ex) {logger.error(ex.getMessage());}finally{if(out!=null)out.close();}} }總結
以上是生活随笔為你收集整理的Jquery 点击当前的标签对象获取值 与JS整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSM项目 Jquery实现From表
- 下一篇: Java Mybatis Xml写法