分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

jquery .map() 和 .each()函数结合使用

发布时间:2023-09-06 02:21责任编辑:郭大石关键词:暂无标签

需求:页面动态添加的html元素(如div),保存时组装div中的数据为一个json对象。

思路:遍历每个div,再遍历div中每个输入元素,把所有先把数据放到一个对象中,再添加进数组,Json.stringfy()序列化。

简单效果图:

点击加号“+”,可以动态添加。

function getCurrentCustomePrice() { ???var cuspricelist = new Array(); ???_.map($("#tabLinePriceType .baojia"), function (n) { ???????var comment = {}; ???????$(n).find("input").each(function () { ???????????switch ($(this).attr("type")) { ???????????????//自定义报价类型 ???????????????case "hidden": ???????????????????comment.custypeid = $(this).val(); ???????????????????break; ???????????????case "checkbox": ???????????????????if ($(this).val() == "0" && $(this).is(":checked")) { ???????????????????????comment.manprice = 1; ???????????????????????break; ???????????????????} ???????????????????else if ($(this).val() == "1" && $(this).is(":checked")) { ???????????????????????comment.childprice = 1; ???????????????????????break; ???????????????????} ???????????????????break; ???????????????case "text": ???????????????????switch ($(this).attr("name").substring($(this).attr("name").lastIndexOf(‘.‘) + 1, $(this).attr("name").length)) { ???????????????????????case "price": ??//成人同行价 ???????????????????????????comment.price = $(this).val(); break; ???????????????????????case "pricechild": ?//儿童同行价 ???????????????????????????comment.pricechild = $(this).val(); break; ???????????????????????case "mprice": ??//成人市场价 ???????????????????????????comment.mprice = $(this).val(); break; ???????????????????????case "mpricechild": //儿童市场价 ???????????????????????????comment.mpricechild = $(this).val(); break; ???????????????????????case "sprice"://网上优惠价 ???????????????????????????comment.sprice = $(this).val(); break; ???????????????????????case "spricechild": ??//儿童网上优惠价 ???????????????????????????comment.spricechild = $(this).val(); break; ???????????????????} ???????????????????break; ???????????} ???????????????}); ???????cuspricelist.push(comment);  //加入数组 ???}) ???return cuspricelist;}

  

jquery .map() 和 .each()函数结合使用

原文地址:https://www.cnblogs.com/meng9527/p/9920620.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved