0

请参阅以下页面进行现场演示:现场演示

如果从专业中选择“麻醉学”并单击“搜索”,则除计数外一切正常。我正在寻找行数。像这样的东西:

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');
            }
4

3 回答 3

1

如何在 for 循环之外定义计数器变量?:)

于 2013-05-10T19:54:57.030 回答
1

编辑:

一开始我不会这样做,您正在更改每个元素上的 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)     
 })

当您在生成的标记中使用 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
            }
于 2013-05-10T17:59:26.303 回答
1

我真的不明白你想要做什么,请描述你想要达到的目标,条件是 if (i[iVar] == dSpecialtyVal) 和if (i == dSpecialtyVal)and for testandfor iVar循环。

可能的问题:

  • 在第二个条件 ( if (i == dSpecialtyVal)) 中,您不是递增 cnt2++ 而是递增 cnt++。这很奇怪,因为在这种情况下您显示 cnt2,它总是等于 1。
  • 此外,如果您要增加 cnt2,这将是无用的,因为 if (i == dSpecialtyVal)is 在for iVar循环之外,并且 cnt 和 cnt2 在for test 循环开始时被重置为值 1。尝试在陈述var cnt2 = 1;之前移动。for test
于 2013-05-10T18:08:52.633 回答