更改事件 - IE 错误
onchange event - IE error
在我的代码隐藏中,我有 -
tbWhatIfBeginDate.Attributes.Add("onchange", "checkDates(" + tbWhatIfBeginDate.ClientID + ", " + tbWhatIfEndDate.ClientID + ")");
tbWhatIfEndDate.Attributes.Add("onchange", "checkDates(" + tbWhatIfBeginDate.ClientID + ", " + tbWhatIfEndDate.ClientID + ")");
这是我的JavaScript函数 -
function checkDates(BeginDateId, EndDateId) {
if (BeginDateId.value > EndDateId.value) {
var beginDt = new Date(BeginDateId.value);
var endDt = new Date(EndDateId.value);
var newDt = new Date(endDt.getTime() - (24 * 60 * 60 * 1000));
var y = newDt.getFullYear(),
m = newDt.getMonth() + 1, // january is month 0 in javascript
d = newDt.getDate();
BeginDateId.value = [pad(m), pad(d), y].join("/");
}
}
当我运行Visual Studio 2010时,它可以工作。
部署到测试服务器时,收到一条错误消息。"预期对象 - 行:176,字符:1"
176行是 - input name="ctl00$cpMain$tbWhatIfBeginDate" type="text" value="8/1/2012" id="ctl00_cpMain_tbWhatIfBeginDate" onchange="checkDates (ctl00_cpMain_tbWhatIfBeginDate, ctl00_cpMain_tbWhatIfEndDate)" style="width:70px;"
我没有看到错误。
想法?
您正在将 HTML 元素的 id 传递给函数,这些元素是字符串,但您传递它时不带引号,就好像它是一个定义的变量一样。在onClick
范围内,没有ctl00_cpMain_tbWhatIfBeginDate
定义这样的变量,所以你得到一个javascript错误。这是意料之中的 - 你还没有声明这样的变量。
然后,在您的函数中,您立即尝试将这些参数视为对象,但同样,您传递了undefined
,因为具有指定名称的变量不存在。也不应该!当您尝试通过 id 引用 DOM 节点时,您必须使用document
对象中的getElementById
来"获取"相关对象。那么,id只是字符串。
因此,您应该传递那些用引号括起来的字符串。为此,您需要修改添加事件的两行:
tbWhatIfBeginDate.Attributes.Add("onchange", "checkDates('" + tbWhatIfBeginDate.ClientID + "', '" + tbWhatIfEndDate.ClientID + "')");
tbWhatIfEndDate.Attributes.Add("onchange", "checkDates('" + tbWhatIfBeginDate.ClientID + "', '" + tbWhatIfEndDate.ClientID + "')");
这样,字符串 ID 就会传递给您的函数。然后,需要修改您的函数以根据传递的 id 获取 DOM 对象:
function checkDates(BeginDateId, EndDateId) {
beginDate = document.getElementById(BeginDateId);
endDate = document.getElementById(EndDateId);
if (!beginDate || !endDate)
return; // <--- add an error message here?
if (beginDate.value > endDate.value) {
var beginDt = new Date(beginDate.value);
var endDt = new Date(endDate.value);
// existing logic here
}
}
文档
-
document.getElementById
- https://developer.mozilla.org/en/DOM/document.getElementById - JavaScript 函数 - http://www.quirksmode.org/js/function.html
- JavaScript 变量 - http://www.tizag.com/javascriptT/javascriptvariables.php
您是否在卡西尼号服务器和 2003 年的盒子之间穿梭? MS 更改了控制命名,转到 IIS 7/最新版本的 VS。 我会检查呈现的 HTML 中控件的实际变量名称是什么。
- IE错误:无法设置属性'的值;样式':对象为null或未定义
- IE错误,视频加载在颜色框中
- IE 错误窗口.onload 不会始终如一地触发
- 更改事件 - IE 错误
- IE 错误 .JavaScript 运行时错误:对象不支持属性或方法“preventDefault”
- jquery 超大小显示 IE 错误
- IE 错误触发器单击以 2 个按钮
- 带有全局变量的IE错误.如何绕过此错误
- jQuery悬停函数IE错误
- js教程示例出现空白.可能与IE错误有关
- IE错误'是null或者不是一个对象
- 无法授权应用程序:FB.login()上的ie错误
- Ext Js只有IE错误
- 在JavaScript文件中使用非标准字符导致IE错误
- 另一个IE错误:堆栈溢出在第0行,而使用jquery地址或至少ajax
- SVG网格渲染Chrome,Firefox,IE -错误的线对齐-模糊的线
- AngularJS IE错误:SCRIPT 5009: 'JSON_XYZ'是未定义的
- jQuery markingup !以及某些页面的IE错误
- jquery在密码和文本字段之间切换IE错误
- 调试IE错误应为对象