jQuery-奇数整数/字符串问题 [英] JQuery - Odd integer/string issue

查看:64
本文介绍了jQuery-奇数整数/字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在努力,还没有得到.我有以下代码可以加载和显示一些JSON数据:

I''ve been working on this all day long and haven't get it yet. I have this code to load and display some JSON data:

var id = firstLevel.id;
$.each(thirdLevel, function(index4, fourthLevel) {
    $('#'+id+' tr:nth-child('+currentRow+')').append("<td>M "+fourthLevel.male+"</td>");
    $('#'+id+' tr:nth-child('+currentRow+')').append("<td>H "+fourthLevel.herm+"</td>");
    currentRow++;
    console.log(id);
});

应该使用表的ID将新列插入一行(我有很多表,每个表都有不同的ID),但是"id"变量似乎工作不正常,将其打印到控制台,并具有正确的值(第一次迭代中为421)

It's supposed to insert new columns to a row using the id of the table (I have a lot of tables with different ids each one) but the "id" variable doesn't seem to be working right, and I'm printing it to console and it has the right value (421 in the first iteration)

现在,如果我这样做:

var id = 421;
$.each(thirdLevel, function(index4, fourthLevel) {
    $('#'+id+' tr:nth-child('+currentRow+')').append("<td>M "+fourthLevel.male+"</td>");
    $('#'+id+' tr:nth-child('+currentRow+')').append("<td>H "+fourthLevel.herm+"</td>");
    currentRow++;
    console.log(id);
});

它将起作用,并将所有新列插入ID为421的表...

It will work, and insert all the new columns on the table with id 421...

那么,我的代码有问题吗? 非常感谢你!

So, is there something wrong with my code? Thank you very much!

推荐答案

首先

First of all numeric IDs are invalid. So do not use them..

第二,确保firstLevel.id没有尾随空格或前导空格.

Secondly make sure the firstLevel.id does not have trailing or leading whitespace ..

尝试console.log(id, id.toString().length);并查看length是否确实为3

try console.log(id, id.toString().length); and see if the length is indeed 3

这篇关于jQuery-奇数整数/字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆