我正在尝试为此代码创建一个if语句,以仅允许具有经验级别1-3的记录显示但不确定如何执行此操作?这是部分代码,如果需要更多让我知道。
var table = document.getElementById('blueteam');
table.innerHTML = ""; //Clears table
if(explevel < 4){
for (var i = 0; i < firstnames.length; i += 1) {
var row = table.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = firstnames[i] + " " + lastnames[i] + " Exp:" + explevel[i];
}
}
};
答案 0 :(得分:3)
请尝试以下代码:
var table = document.getElementById('blueteam');
table.innerHTML = ""; //Clears table
for (var i = 0; i < firstnames.length; i += 1) {
if(explevel[i] >= 1 && explevel[i] <= 3){
var row = table.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = firstnames[i] + " " + lastnames[i] + " Exp:" + explevel[i];
}
}
};
如果explevel < 4
您正在通话,但由于explevel
是一个列表(explevel[i]
),您无法做到。相反,请检查explevel
的每次迭代。
答案 1 :(得分:1)
var table = document.getElementById('blueteam');
table.innerHTML = ""; //Clears table
var count = records.length;
var record;
for (var i = 0; i < count; i++) {
record = records[i];
if (reecord.explevel < 4) {
var row = table.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = record.firstname + " " + record.lastname + " Exp:" + record.explevel;
}
}
我修改了数据。我不确定firstnames
,lastnames
和explevel
。我希望那些应该出现在record
。
示例数据
var records = [{
"firstname": "",
"lastname": "",
"explevel": ""
}];
答案 2 :(得分:1)
您可以使用逻辑AND (&&)
运算符:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
var table = document.getElementById('blueteam');
table.innerHTML = ""; //Clears table
for (var i = 0; i < firstnames.length; i++) {
if((explevel[i] >= 1) && (explevel[i] <= 3)){
var row = table.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = firstnames[i] + " " + lastnames[i] + " Exp:" + explevel[i];
}
}