变量和对象的问题
我有 3 个 TextFields
,名为 txtUSD
、txtEUR
、txtAUS
。和具有相同值的 PopupList
,减去 txt
部分,但我需要根据选择形成要使用的 TextFields
的名称用户制作的。所以我这样做了:
function btConvert_Click(event)
{
var amount = document.getElementById("txtAmount").value;
var rates = document.getElementById("lstConvertTo").value;
var from = "txt" + document.getElementById("lstFrom").options[document.getElementById('lstFrom').selectedIndex].text;
var to = "txt" + document.getElementById("lstConvertTo").options[document.getElementById("lstConvertTo").selectedIndex].text;
var curr_from = document.getElementById(from).value;
var curr_to = document.getElementById(to).value;
if(curr_from > curr_to)
{
amount * rates;
} else {
amount / rates;
}
alert(result);
}
但每次尝试时都会收到此错误:
mobile/main.js 第 215 行:表达式“document.getElementById(from)”的结果 [null] 不是对象。
我该怎么做呢?
I have 3 TextFields
, called txtUSD
, txtEUR
, txtAUS
. And a PopupList
with the same values, minus the txt
part, but I need to form the names of the TextFields
to use based on the selection that the user made. So I've done this:
function btConvert_Click(event)
{
var amount = document.getElementById("txtAmount").value;
var rates = document.getElementById("lstConvertTo").value;
var from = "txt" + document.getElementById("lstFrom").options[document.getElementById('lstFrom').selectedIndex].text;
var to = "txt" + document.getElementById("lstConvertTo").options[document.getElementById("lstConvertTo").selectedIndex].text;
var curr_from = document.getElementById(from).value;
var curr_to = document.getElementById(to).value;
if(curr_from > curr_to)
{
amount * rates;
} else {
amount / rates;
}
alert(result);
}
But every time I try it I get this error:
mobile/main.js line 215: Result of expression 'document.getElementById(from)' [null] is not an object.
How should I make it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您收到的错误来看,生成
from
变量时似乎存在错误。为了简洁起见,您应该考虑将
document.getElementById('lstFrom')
存储到它自己的 var 中。From the error you're getting, it looks like there's a bug when generating the
from
variable.You should consider storing
document.getElementById('lstFrom')
into it's own var, for brevity.