javascript函数有时会在更改时失败

时间:2013-05-08 11:11:23

标签: function onchange

所以我有这个网页代码,它在Firefox中有效,但只有选择适用于IE8。为了使其在Firefox中工作,警报(null)必须存在,否则它将不会转到href时给出的myloc == "MySrch"。我没有收到任何反馈错误,这实在令人困惑。

SqlStr = "Select DISTINCT Unit From Employee Where PhoneBook = 1 Order by Unit;"
Set UniNme = Connection.Execute(SqlStr)
Response.Write "<select id='Unit' onChange='Change(this.id)'>"
Response.Write "<option value=''>Choose Unit</option><br>"
Response.Write "<option value='Everyone'>Everyone</option><br>"
Do While Not UniNme.EOF
Response.Write "<option value='"&UniNme("Unit")&"'>"&UniNme("Unit")&"</option><br>"
UniNme.MoveNext
Loop
Response.Write "</select><br>"

Response.Write "Name Search: <input type='text' id='MySrch' onChange='Change(this.id)'>"

javascript函数是

function Change(myloc) {
if (myloc == "Unit") {
var myindex = document.getElementById(myloc).selectedIndex;
myvalue = document.getElementsByTagName("option")[myindex].value;
} else {
var myvalue = document.getElementById(myloc).value;
}

document.cookie = "Temp1="+escape(myloc);
document.cookie = "Temp2="+escape(myvalue);

document.location.href="AEmployees.asp?action=Change";

alert(null);
}

1 个答案:

答案 0 :(得分:0)

看一下myvalue变量的范围。它在这里具有本地可见性

else {
var myvalue = document.getElementById(myloc).value;
}

它可能不会导致问题,但它可能是危险的代码