为什么用js将表格中的数据拼接成json格式不能够以json的用法使用,小白求教

simpletonQ 2017-07-07 09:29:47
$('#dq').click(function (e) {
e.preventDefault();
var int=$('td input');
var json="[";
var tdValue;
for(var i=0;i<int.length;i++){
if($(int[i]).prop('checked')==true){
// console.log($(int[i]))
var checktr=$(int[i]).parent().parent();
// console.log(checktr);
var tds=checktr.children()
console.log(checktr.children())
var keyArr=$('th');
json +="{";
for(var j=1;j<tds.length;j++){
tdName=$(keyArr[j]).html();
console.log($(tdName))
json+="\"";
json+=tdName;
json+="\"";
json+=":";
tdValue=$(tds[j]).html();
json+="\"";
json+=tdValue;
json+="\"";
json+=","

}
json=json.substring(0,json.length-1);
json+="}";
json+=","
}
}
json=json.substring(0,json.length-1);
json+="]";
document.write(json[0]);
...全文
255 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
2017-07-08
  • 打赏
  • 举报
回复
这样拼出来的是字符串,需要eval或者parseJSON转换成对象
simpletonQ 2017-07-07
  • 打赏
  • 举报
回复
从table表格中选中两行数据,读取出来保存为json数组的形式,为什么我这样拼出来的json数组不能具有数据下标选中读取的功能
simpletonQ 2017-07-07
  • 打赏
  • 举报
回复
哥哥,是不是这样拼,json就会是对象数组了
2017-07-07
  • 打赏
  • 举报
回复
你这样拼很容易出错

var json = [];
for(var i = 0; i < 3; i++){
	var item = {};
	var tdName = "name" + i, tdValue = "value" + i;
	item[tdName] = tdValue;
	json.push(item);
}
console.log(json[0]);
xwn_2016 2017-07-07
  • 打赏
  • 举报
回复
为啥这么拼呢, 用 var obj = new Object(); obj['a']=1; obj["b"]=2; 这样应该没问题

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧