我正在尝试使用HTML下拉列表(<select>
)来引用对象列表(例如Emp_2
)。然后,我想使用所选对象并访问其TCL
属性。我正在连接选定的选项值结果(使用 .TCL )以引用所选对象属性)。除了getElementById('id')
返回字符串值而不是对象之外,一切似乎都有效。字符串emp
(连接后)以正确的名称(Emp_2.TCL
)结束,但不引用我的对象属性(Emp_2.TCL
)。
var Emp_1 ={fname: "Mark", lname: "Arm", wage: 12.00, OT_wage: 18.00, TCL: 25.53 };
var Emp_2 ={fname: "Ed", lname: "Del", wage: 15.00, OT_wage: 28.00, TCL: 25.00 };
var Emp_3 ={fname: "Filen", lname: "Haro", wage: 16.00, OT_wage: 24.00, TCL: 20.00 };
var Emp_4 ={fname: "Javi", lname: "Loz", wage: 15.00, OT_wage: 22.00, TCL: 26.99};
function tech_select(x=""){
var num_of_emp = 5;
var sel = '<select id="tech_selector_'+x+'"> \n ';
sel += '<option value="select" selected="selected">Select Technician</option> \n ';
i=1;
while(i<num_of_emp){
var emp = 'Emp_'+i;
sel += '<option value="'+emp+'">' + eval('Emp_'+i+'.fname') + ' ' + eval('Emp_'+i+'.lname') + '</option> \n ';
i++;
}
sel += '</select> \n ';
return sel;
}
function emp_tcl_total(row_num=2){
var r = row_num;
var emp ;
emp = document.getElementById('tech_selector_'+r).value;
emp += '.TCL'; //This is building a string**"Emp_2.TCL"** rather then the desired object named **Emp_2.TCL**.
document.getElementById('tcl_'+r).innerHTML = emp; // emp needs to be an object name not a string. Emp_2.TCL is an object
alert(emp); //returns Emp_2.TCL
alert(Emp_2.TCL); //returns 25
}
答案 0 :(得分:0)
使用数组而不是单独的变量:
var Emp = [];
Emp['someindex'] ={fname: "Mark", lname: "Arm", wage: 12.00, OT_wage: 18.00, TCL: 25.53 };
...然后根据选择索引值查找数据。