请参阅以下页面了解实时演示:Live Demo
如果从By Specialty中选择“麻醉学”并点击“搜索”,除计数外,一切正常。我想要计算行数。像这样:
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');
}
答案 0 :(得分: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
}
答案 1 :(得分:1)
我真的不明白你要做什么,请描述你想要达到的条件if(i [iVar] == dSpecialtyVal)和if (i == dSpecialtyVal)
以及{{1} }和for test
循环。
可能的问题:
for iVar
)中,你不是
递增cnt2 ++但是cnt ++。这是奇怪的,因为你显示cnt2
这种情况,总是等于1。if (i == dSpecialtyVal)
超出了if (i == dSpecialtyVal)
循环和cnt和
cnt2重置为for iVar
开头的值1
环。尝试在for test
声明之前移动var cnt2 = 1;
。答案 2 :(得分:1)
如何在for循环外定义计数器变量? :)