Global variable in Javascript, rerurn wrong value -
i have problem program. had "tempname" variable. global variable. set value in "ktdn" function , value in function "loadmain" has "undefined". please me!! much!!
var tempname; var tempposititon; function ktdn() { for(var x=0; x < mangnhanvien.length; x++) { if(document.getelementbyid("txtuser").value == mangnhanvien[x].user && document.getelementbyid("txtpass").value == mangnhanvien[x].pass) { tempname = mangnhanvien[x].ten; tempposititon = mangnhanvien[x].position; alert(tempname +" "+tempposititon);//debug ==> right value //alert("dang nhap thanh cong"); window.open("../html/main.html"); window.open('','_self').close(); return true; } } alert("tai khoan dang nhap khong chinh xac"); } function logout() { window.open("../html/login.html"); window.open('','_self').close(); } function loadmain() { document.getelementbyid("lbluser").innerhtml = tempname; //==> wrong value, "undefined" document.getelementbyid("lblposition").innerhtml = tempposititon; var s = "<label for='lstnv'>tìm kiếm:</label><input list='lstnv'><datalist id='lstnv'>"; for( x in mangnhanvien) { s += "<option value='"+mangnhanvien[x].ten+"'></option>"; } s += "</datalist>"; document.getelementbyid("timnv").innerhtml = s; var tbl = "<table><tr><th>stt</th><th>hỌ tÊn</th><th>giỚi tÍnh</th><th>ngÀy sinh</th><th>cmnd</th><th>ĐỊa chỈ</th><th>sỐ ĐiỆn thoẠi</th><th>ngÀy vÀo lÀm</th><th>ngÀy nghỈ</th></tr>" for( x in mangnhanvien) { tbl += "<tr><td>"+mangnhanvien[x].maso+"</td><td>"+mangnhanvien[x].ten+"</td><td>"+mangnhanvien[x].gioitinh+"</td><td>"+mangnhanvien[x].ngaysinh+"</td><td>"+mangnhanvien[x].cmnd+"</td><td>"+mangnhanvien[x].diachi+"</td><td>"+mangnhanvien[x].sdt+"</td><td>"+mangnhanvien[x].ngayvaolam+"</td><td>"+mangnhanvien[x].ngaynghi+"</td></tr>"; } tbl += "</table>"; document.getelementbyid("tblnv").innerhtml = tbl; }
you need make sure ktdn function must called before use global variable value in loadmain function.
also use default value tempname like:
var tempname = 'default value';
Comments
Post a Comment