Javascript JQuery中的行计数

Javascript JQuery中的行计数,javascript,jquery,Javascript,Jquery,请参阅以下页面以获取实时演示: 如果按专业选择“麻醉学”并单击“搜索”,则除计数外,所有操作都正常。我正在数行。大概是这样的: 1 2 3 4 5 6 7 但我得到的只是: 1 1 1 1 1 1 1 我有两个变量,CNT和CNT2,我使用CNT++和CNT2++来递增,但它不起作用 for (test = 0; test <= phyList.length - 1; test++) { i = phyList[test].spe

请参阅以下页面以获取实时演示:

如果按专业选择“麻醉学”并单击“搜索”,则除计数外,所有操作都正常。我正在数行。大概是这样的:

1
2
3
4
5
6
7
但我得到的只是:

1
1
1
1
1
1
1
我有两个变量,CNT和CNT2,我使用CNT++和CNT2++来递增,但它不起作用

            for (test = 0; test <= phyList.length - 1; test++) {
                i = phyList[test].specialty; //get all specialty in the array
                var cnt = 1;
                var cnt2 = 1;
                for (var iVar = 0; iVar < i.length; iVar++) {
                    if (i[iVar] == dSpecialtyVal) { //$(".dSpecialty").find('option:selected').attr('id')) { //if what's in the phyList array matches selection
                        recs += "<tr><td class=lborder>";
                        recs += cnt + "</td><td class=lborder>";
                        recs += phyList[test].firstName + "</td><td class=lborder>";
                        recs += phyList[test].lastName + "</td><td class=lborder>";
                        recs += phyList[test].title + "</td><td class=lborder>";
                        recs += phyList[test].specialty[iVar] + "</td><td class=lborder>";
                        recs += phyList[test].address + "</td><td class=lborder>";
                        recs += phyList[test].phone + "</td>";
                        recs += '</tr>';
                        $('.displayresult tbody').html(recs);
                        document.getElementById('errorsp').innerHTML = "<i>Match found</i>";
                    }
                    cnt++;
                }
                if (i == dSpecialtyVal){ //$(".dSpecialty").find('option:selected').attr('id')) { //if what's in the phyList array matches selection
                    recs += "<tr><td class=lborder>";
                    recs += cnt2 + "</td><td class=lborder>";
                    recs += phyList[test].firstName + "</td><td class=lborder>";
                    recs += phyList[test].lastName + "</td><td class=lborder>";
                    recs += phyList[test].title + "</td><td class=lborder>";
                    recs += phyList[test].specialty + "</td><td class=lborder>";
                    recs += phyList[test].address + "</td><td class=lborder>";
                    recs += phyList[test].phone + "</td>";
                    recs += '</tr>';
                    $('.displayresult tbody').html(recs);
                    document.getElementById('errorsp').innerHTML = "<i>Match found</i>";
                    cnt2++;
                }
                $("#splabel").css('font-weight', 'bold');
                $("#fname").css('font-weight', 'normal');
                $("#lname").css('font-weight', 'normal');
            }
for(test=0;test编辑:

首先,我不会这样做,你在每个元素上都改变了DOM,这是件坏事,你还把搜索和生成标记混为一谈

考虑以下被剪掉的部分

 var physList = { ... }     

 function generateRow(phys, index) {
     return "<tr><td class=lborder>";
            + index + "</td><td class=lborder>";
            +phys.firstName + "</td><td class=lborder>";
            +phys.lastName + "</td><td class=lborder>";
            +phys.title + "</td><td class=lborder>";
            +phys.specialty[iVar] + "</td><td class=lborder>";
            +phys.address + "</td><td class=lborder>";
            +phys.phone + "</td>";
            +"</tr>";
 }

 function getPhysBySpecialty(specialty) {
      return $.grep(physList, function (phys, index) {
          return phys.specialty == specialty
      }
 }

 ....

 $('.sButton').on('click',function() {

     var filtered = getPhysBySpecialty(specialty)
       , rows = $.map(filtered, generateRow)
       , html = rows.join('')

     $('.displayresult').find('tbody').html(html)     
 })
var physList={…}
函数生成器OW(物理,索引){
返回“”;
+索引+“”;
+phys.firstName+“”;
+phys.lastName+“”;
+phys.title+“”;
+物理专业[iVar]+“”;
+物理地址+“”;
+物理电话+“”;
+"";
}
函数GetPhysBySpecial(专业){
return$.grep(physList,函数(phys,索引){
返回物理专业==专业
}
}
....
$('.sButton')。在('click',function()上{
var filtered=getPhysBySpecialty(专业)
,行=$.map(已筛选,generateRow)
,html=rows.join(“”)
$('.displayresult').find('tbody').html(html)
})
在生成的标记中使用cnt2时,可以在两个位置增加cnt,而不增加cnt2的位置

//Place the initialization outside the loop
                var cnt = 1;
                var cnt2 = 1;
             for (test = 0; test <= phyList.length - 1; test++) {
                i = phyList[test].specialty; //get all specialty in the array
                for (var iVar = 0; iVar < i.length; iVar++) {
                    if (i[iVar] == dSpecialtyVal) { //$(".dSpecialty").find('option:selected').attr('id')) { //if what's in the phyList array matches selection
                        recs += "<tr><td class=lborder>";
                        recs += cnt + "</td><td class=lborder>";
                        recs += phyList[test].firstName + "</td><td class=lborder>";
                        recs += phyList[test].lastName + "</td><td class=lborder>";
                        recs += phyList[test].title + "</td><td class=lborder>";
                        recs += phyList[test].specialty[iVar] + "</td><td class=lborder>";
                        recs += phyList[test].address + "</td><td class=lborder>";
                        recs += phyList[test].phone + "</td>";
                        recs += '</tr>';
                        $('.displayresult tbody').html(recs);
                        document.getElementById('errorsp').innerHTML = "<i>Match found</i>";
                    }
                }
                if (i == dSpecialtyVal){   //$(".dSpecialty").find('option:selected').attr('id')) { //if what's in the phyList array matches selection
                    recs += "<tr><td class=lborder>";
                    recs += cnt2 + "</td><td class=lborder>";
                    recs += phyList[test].firstName + "</td><td class=lborder>";
                    recs += phyList[test].lastName + "</td><td class=lborder>";
                    recs += phyList[test].title + "</td><td class=lborder>";
                    recs += phyList[test].specialty + "</td><td class=lborder>";
                    recs += phyList[test].address + "</td><td class=lborder>";
                    recs += phyList[test].phone + "</td>";
                    recs += '</tr>';
                    $('.displayresult tbody').html(recs);
                    document.getElementById('errorsp').innerHTML = "<i>Match found</i>";
                }
                $("#splabel").css('font-weight', 'bold');
                $("#fname").css('font-weight', 'normal');
                $("#lname").css('font-weight', 'normal');
                    cnt++;cnt2++; // increment only at the end
            }
//将初始化置于循环之外
var-cnt=1;
var-cnt2=1;
对于(测试=0;测试编辑:

首先,我不会这样做,你在每个元素上都改变了DOM,这是件坏事,你还把搜索和生成标记混为一谈

考虑以下被剪掉的部分

 var physList = { ... }     

 function generateRow(phys, index) {
     return "<tr><td class=lborder>";
            + index + "</td><td class=lborder>";
            +phys.firstName + "</td><td class=lborder>";
            +phys.lastName + "</td><td class=lborder>";
            +phys.title + "</td><td class=lborder>";
            +phys.specialty[iVar] + "</td><td class=lborder>";
            +phys.address + "</td><td class=lborder>";
            +phys.phone + "</td>";
            +"</tr>";
 }

 function getPhysBySpecialty(specialty) {
      return $.grep(physList, function (phys, index) {
          return phys.specialty == specialty
      }
 }

 ....

 $('.sButton').on('click',function() {

     var filtered = getPhysBySpecialty(specialty)
       , rows = $.map(filtered, generateRow)
       , html = rows.join('')

     $('.displayresult').find('tbody').html(html)     
 })
var physList={…}
函数生成器OW(物理,索引){
返回“”;
+索引+“”;
+phys.firstName+“”;
+phys.lastName+“”;
+phys.title+“”;
+物理专业[iVar]+“”;
+物理地址+“”;
+物理电话+“”;
+"";
}
函数GetPhysBySpecial(专业){
return$.grep(physList,函数(phys,索引){
返回物理专业==专业
}
}
....
$('.sButton')。在('click',function()上{
var filtered=getPhysBySpecialty(专业)
,行=$.map(已筛选,generateRow)
,html=rows.join(“”)
$('.displayresult').find('tbody').html(html)
})
在生成的标记中使用cnt2时,可以在两个位置增加cnt,而不增加cnt2的位置

//Place the initialization outside the loop
                var cnt = 1;
                var cnt2 = 1;
             for (test = 0; test <= phyList.length - 1; test++) {
                i = phyList[test].specialty; //get all specialty in the array
                for (var iVar = 0; iVar < i.length; iVar++) {
                    if (i[iVar] == dSpecialtyVal) { //$(".dSpecialty").find('option:selected').attr('id')) { //if what's in the phyList array matches selection
                        recs += "<tr><td class=lborder>";
                        recs += cnt + "</td><td class=lborder>";
                        recs += phyList[test].firstName + "</td><td class=lborder>";
                        recs += phyList[test].lastName + "</td><td class=lborder>";
                        recs += phyList[test].title + "</td><td class=lborder>";
                        recs += phyList[test].specialty[iVar] + "</td><td class=lborder>";
                        recs += phyList[test].address + "</td><td class=lborder>";
                        recs += phyList[test].phone + "</td>";
                        recs += '</tr>';
                        $('.displayresult tbody').html(recs);
                        document.getElementById('errorsp').innerHTML = "<i>Match found</i>";
                    }
                }
                if (i == dSpecialtyVal){   //$(".dSpecialty").find('option:selected').attr('id')) { //if what's in the phyList array matches selection
                    recs += "<tr><td class=lborder>";
                    recs += cnt2 + "</td><td class=lborder>";
                    recs += phyList[test].firstName + "</td><td class=lborder>";
                    recs += phyList[test].lastName + "</td><td class=lborder>";
                    recs += phyList[test].title + "</td><td class=lborder>";
                    recs += phyList[test].specialty + "</td><td class=lborder>";
                    recs += phyList[test].address + "</td><td class=lborder>";
                    recs += phyList[test].phone + "</td>";
                    recs += '</tr>';
                    $('.displayresult tbody').html(recs);
                    document.getElementById('errorsp').innerHTML = "<i>Match found</i>";
                }
                $("#splabel").css('font-weight', 'bold');
                $("#fname").css('font-weight', 'normal');
                $("#lname").css('font-weight', 'normal');
                    cnt++;cnt2++; // increment only at the end
            }
//将初始化置于循环之外
var-cnt=1;
var-cnt2=1;

对于(test=0;test我真的不明白您试图做什么,请描述您试图达到的目标,条件是(I[iVar]==dSpecialtyVal)和
if(I==dSpecialtyVal)
对于test
对于iVar
循环

可能的问题:

  • 在第二种情况下(
    如果(i==dsspecialtyval)
    ),您不是 增加cnt2+,但增加cnt++。这很奇怪,因为您在中显示cnt2 这种情况下,它总是等于1
  • 此外,如果您增加cnt2,它将是无用的,因为
    如果(i==dsspecialtyval)
    超出了iVar的
    循环和cnt的
    ,并且
    cnt2在测试的
    开始处重置为值1
    循环。尝试在test
    语句的
    之前移动
    var cnt2=1;

我真的不明白您试图做什么,请描述您试图达到的目标,条件是(I[iVar]==dSpecialtyVal)和
if(I==dSpecialtyVal)
对于test
对于iVar
循环

可能的问题:

  • 在第二种情况下(
    如果(i==dsspecialtyval)
    ),您不是 增加cnt2+,但增加cnt++。这很奇怪,因为您在中显示cnt2 这种情况下,它总是等于1
  • 此外,如果您增加cnt2,它将是无用的,因为
    如果(i==dsspecialtyval)
    超出了iVar的
    循环和cnt的
    ,并且
    cnt2在测试的
    开始处重置为值1
    循环。尝试在test
    语句的
    之前移动
    var cnt2=1;


在for循环之外定义计数器变量怎么样?:)

在for循环之外定义计数器变量怎么样?:)

你似乎没有在任何地方增加
cnt2
,或者我遗漏了什么?不,这是真的。我知道它可以被修剪,但被另一个程序员从这里推荐。因为除了行计数之外,它不起作用。你似乎没有在任何地方增加
cnt2
,如果你在外部
For
循环中增加它,你将只需在每一步将其值重置为1。我只是想显示行数。仅此而已。您似乎没有在任何地方递增
cnt2
,或者我遗漏了什么?不,这是真实的。我知道它可以被修剪,但是由另一个程序员从这里推荐的。因为除了行数之外,它没有工作。您似乎没有递增
cnt2
任意位置,如果您在外部
for
循环中增加它,您只需在每一步将其值重置为1。我只是想显示行数。仅此而已。抱歉,修复了代码。为什么这会被否决?已更新,但它给了我所有“15”现在:/@vittore,请查看我的编辑。我想我发现了一个小错误。无法正常工作。我如何重写代码以使行计数正常工作?抱歉,修复了代码。为什么这会被否决?已更新,但它给了我全部“15”现在:/@vittore,请检查我的编辑。我想我发现了一个小错误。无法正常工作。我如何重写代码以使行数正常工作?我只是希望每增加一行,就增加一个计数。我是