我想在DOM完全加载后设置对象的值。问题是,在从文本框调用OnBlur
和OnFocus
事件后,我收到空指针异常。我做错了什么?
的javascript:
$(document).ready(function () {
var sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last()
});
var tempThursdayHrs = 0.0;
function BlurThursdayHrs(sender, args) { sumThursdayHrs.text(tempThursdayHrs + sender.get_value()); }
function FocusThursdayHrs(sender, args) { tempThursdayHrs = sumThursdayHrs.text() - sender.get_value(); }
标记:
<telerik:RadNumericTextBox ID="txtThursdayHrs" runat="server" NumberFormat-DecimalDigits="1"
Width="25px" MinValue="0" Type="Number" DbValue='<%# Eval("ThursdayHrs") %>'>
<ClientEvents OnBlur="BlurThursdayHrs" OnFocus="FocusThursdayHrs" />
</telerik:RadNumericTextBox>
错误:
Microsoft JScript运行时错误:'sumThursdayHrs'未定义
答案 0 :(得分:3)
正如您在标题中所说,值在函数中设置(就绪事件处理函数)。但JavaScript具有函数范围,并且当您使用sumThursdayHrs
关键字将var
声明为本地(不是说“私有”)变量时,它是从外部未定义的。两个可能性:
答案 1 :(得分:2)
如果你使用jquery已经为什么不用jquery绑定事件?
$('span[id$="txtThursdayHrs"]:first').on("focus", FocusThursdayHrs)
.on("blur", BlurThursdayHrs);
另外,在doc ready函数中声明这些函数,这样他们就可以访问sumThursdayHrs
,或者在同一个地方使用匿名函数而不是调用函数
例如:
$(document).ready(function () {
var sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last();
var tempThursdayHrs = 0.0;
$('span[id$="txtThursdayHrs"]:first').on("focus", function() {
sumThursdayHrs.text(tempThursdayHrs + $(this).val());
})
.on("blur", function() {
tempThursdayHrs = sumThursdayHrs.text() - $(this).val();
});
});
或者将sumThursdayHrs
移动到全局对象......为了理智,我建议不要把它放在窗口中。
而是定义一个全局命名空间来保存特定于应用程序的全局变量。这被称为全球减排。
实施例
var MyApp = {};
$(document).ready(function () {
MyApp.sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last();
//...
-
MyApp.tempThursdayHrs = 0.0;
function BlurThursdayHrs(sender, args) { MyApp.sumThursdayHrs.text(MyApp.tempThursdayHrs + sender.get_value()); }
function FocusThursdayHrs(sender, args) { MyApp.tempThursdayHrs = MyApp.sumThursdayHrs.text() - sender.get_value(); }
答案 2 :(得分:1)
错误是由范围界定引起的:
$(document).ready(function () {
// sumThursdayHrs is a *local* variable to the function
var sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last()
});
// ... so this will fail because it is trying to use
// window.sumThursdayHrs (the property sumThursdayHrs on the window object),
// which is not set and thus the Reference Error
function BlurThursdayHrs(sender, args) {
sumThursdayHrs.text(tempThursdayHrs + sender.get_value());
}
“简单修复”(可能有效也可能无效)是:
$(document).ready(function () {
// Set the "global variable" sumThursdayHrs
// "window." isn't technically required, but it is to show a point.
window.sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last()
});
快乐的编码。
答案 3 :(得分:0)
我不确定你提供的代码示例是否在同一个包中,但是如果你在$(document).ready()
中定义了一个变量,那么知道该变量在一个闭包内。除此之外,它将是未定义的。
那么为什么不这样做:
$(document).ready(function () {
var sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last();
var tempThursdayHrs = 0.0;
function BlurThursdayHrs(sender, args) {
sumThursdayHrs.text(tempThursdayHrs + sender.get_value());
}
function FocusThursdayHrs(sender, args) {
tempThursdayHrs = sumThursdayHrs.text() - sender.get_value();
}
});
在上面的代码中,它应该选择你想要的值,因为所有内容都在$(document).ready()
闭包内。此外,你已经将一个jquery对象$('span[id*="lblThursdayHrs"]')
分配给一个变量,所以它必须在jquery环境中使用而不是在外面...在一个单词中,如果你使用的是jquery,那么保持它jquery环境。
你所做的是,你已经与jquery签订了一份合同,你将通过var sumThursdayHrs = $('span[id*="lblThursdayHrs"]').last()
使用它的所有方法和属性。一旦你使用$(something)
,就是它,它必须在jquery环境中使用。