C# 为什么这个JavaScript不显示确认框?
我正在尝试修复我继承的产品中的一些bug,我有一段javascript代码,它应该点亮几个框并弹出一个确认框。现在发生的事情是,我看到盒子改变颜色,有5秒左右的延迟,然后它就好像丢失的确认只接受自己。有没有比我聪明的人认为这段代码有什么问题C# 为什么这个JavaScript不显示确认框?,c#,.net,asp.net,javascript,C#,.net,Asp.net,Javascript,我正在尝试修复我继承的产品中的一些bug,我有一段javascript代码,它应该点亮几个框并弹出一个确认框。现在发生的事情是,我看到盒子改变颜色,有5秒左右的延迟,然后它就好像丢失的确认只接受自己。有没有比我聪明的人认为这段代码有什么问题 function lastCheckInv() { document.getElementById("ctl00$ContentPlaceHolderMain$INDet$txtInvNumber").style.background = "yel
function lastCheckInv() {
document.getElementById("ctl00$ContentPlaceHolderMain$INDet$txtInvNumber").style.background = "yellow";
document.getElementById("ctl00$ContentPlaceHolderMain$INDet$txtInvNumber").focus();
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_AddCharges").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
bRetVal = confirm("Are you sure the information associated with this invoice is correct?");
return bRetVal;
}
我能想到的唯一一件事是,如果确认之前的一行抛出了异常,而你却永远无法真正得到确认 如果您正在使用IE,请确保已启用脚本调试。如果您使用的是Firefox,请安装Firebug插件并为您的网站启用它
或者,对于非常原始的调试,只需在每条语句后添加警报,并找出它的爆炸位置。我唯一能想到的是,如果确认前的一行抛出异常,而您实际上从未到达确认 如果您正在使用IE,请确保已启用脚本调试。如果您使用的是Firefox,请安装Firebug插件并为您的网站启用它
或者,对于非常原始的调试,只需在每条语句后添加警报,并找出它的爆炸点。一些想法:可能是.focus调用将确认隐藏在页面后面?或者可能是某个控件id不正确导致.style.background引用失败 您应该在显示确认框后设置焦点,否则确认框将抓住焦点,使该行失去意义。 不要硬编码ASP.Net id是这样的。虽然它们通常是一致的,但ASP.net的未来版本不能保证相同的方案,这意味着您在将来某个时候更新此代码时会感到痛苦。使用控件的server-side.ClientID属性将这些值作为可以引用的变量写入javascript中。 更新:
根据您对另一个响应的注释,如果函数返回true,则此代码将导致回发。在这种情况下,运行.focus行毫无意义,除非返回false。一些想法:可能是.focus调用将确认隐藏在页面后面?或者可能是某个控件id不正确导致.style.background引用失败 您应该在显示确认框后设置焦点,否则确认框将抓住焦点,使该行失去意义。 不要硬编码ASP.Net id是这样的。虽然它们通常是一致的,但ASP.net的未来版本不能保证相同的方案,这意味着您在将来某个时候更新此代码时会感到痛苦。使用控件的server-side.ClientID属性将这些值作为可以引用的变量写入javascript中。 更新:
根据您对另一个响应的注释,如果函数返回true,则此代码将导致回发。在这种情况下,运行.focus行毫无意义,除非返回false。您应该使用以下方法从JavaScript引用控件:
document.getElementById(<%= txtInvNumber.ClientID %>).style.background = "yellow"
如果这没有帮助,请尝试在JavaScript中设置断点,并逐步查看其失败的位置。您应该使用以下方法从JavaScript引用控件:
document.getElementById(<%= txtInvNumber.ClientID %>).style.background = "yellow"
如果没有帮助,请尝试在JavaScript中设置一个断点,并逐步查看其失败的地方。此测试脚本在IE、Firefox和Opera中运行良好。因此,您的基本语法似乎没有任何问题。问题要么在于ID不符合事实,即它在5秒后就好像被确认了一样,要么在于页面上其他一些冲突的JavaScript中。如果看不到更多的页面,将很难帮助您
<script language="javascript">
function lastCheckInv() {
document.getElementById("test1").style.background = "yellow";
document.getElementById("test1").focus();
document.getElementById("test2").style.background = "yellow";
document.getElementById("test3").style.background = "yellow";
bRetVal = confirm("Are you sure?");
return bRetVal;
}
</script>
<form method="get" onsubmit="return lastCheckInv();">
<input id="test1" name="test1" value="Hello">
<input id="test2" name="test2" value="Hi">
<input id="test3" name="test3" value="Bye">
<input type="submit" name="Save" value="Save">
</form>
这个测试脚本在IE、Firefox和Opera中运行良好。因此,您的基本语法似乎没有任何问题。问题要么在于ID不符合事实,即它在5秒后就好像被确认了一样,要么在于页面上其他一些冲突的JavaScript中。如果看不到更多的页面,将很难帮助您
<script language="javascript">
function lastCheckInv() {
document.getElementById("test1").style.background = "yellow";
document.getElementById("test1").focus();
document.getElementById("test2").style.background = "yellow";
document.getElementById("test3").style.background = "yellow";
bRetVal = confirm("Are you sure?");
return bRetVal;
}
</script>
<form method="get" onsubmit="return lastCheckInv();">
<input id="test1" name="test1" value="Hello">
<input id="test2" name="test2" value="Hi">
<input id="test3" name="test3" value="Bye">
<input type="submit" name="Save" value="Save">
</form>
我不喜欢通过直接访问对象
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
如果未返回对象,JavaScript将出错。我更喜欢这种方法
var obj = document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight");
if(obj)
{
obj.style.background = "yellow";
}
我猜您正在尝试访问一个不在DOM上的对象,因此它永远不会到达确认调用。我不喜欢直接通过
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
如果未返回对象,JavaScript将出错。我更喜欢这种方法
var obj = document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight");
if(obj)
{
obj.style.background = "yellow";
}
我猜您正在尝试访问一个不在DOM上的对象,因此它永远不会到达确认调用。检查元素是否确实存在可能是值得的。另外,尝试将焦点延迟到确认后:
function lastCheckInv() {
var myObjs,bRetVal;
myObjs=[
"ctl00_ContentPlaceHolderMain_INDet_AddCharges",
"ctl00_ContentPlaceHolderMain_INDet_lblFreight",
"ctl00$ContentPlaceHolderMain$INDet$txtInvNumber"
];
bRetVal = confirm("Are you sure the information associated with this invoice is correct?");
for (var i=0, j=myObjs.length, myElement; i<j; i++){
myElement=document.getElementById(myObjs[i]);
if (!myElement){
// this element is missing
continue;
}
else {
// apply colour
myElement.style.background = "yellow";
// focus the last object in the array
if (i == (j-1) ){
myObj.focus();
}
}
}
return bRetVal;
}
也许值得检查一下 这些元素实际上是存在的。另外,尝试将焦点延迟到确认后:
function lastCheckInv() {
var myObjs,bRetVal;
myObjs=[
"ctl00_ContentPlaceHolderMain_INDet_AddCharges",
"ctl00_ContentPlaceHolderMain_INDet_lblFreight",
"ctl00$ContentPlaceHolderMain$INDet$txtInvNumber"
];
bRetVal = confirm("Are you sure the information associated with this invoice is correct?");
for (var i=0, j=myObjs.length, myElement; i<j; i++){
myElement=document.getElementById(myObjs[i]);
if (!myElement){
// this element is missing
continue;
}
else {
// apply colour
myElement.style.background = "yellow";
// focus the last object in the array
if (i == (j-1) ){
myObj.focus();
}
}
}
return bRetVal;
}
布雷特瓦尔真的在什么地方申报了吗
如果没有,var bRetVal=确认。。。。是告诉jscript它是一个变量的友好方式。bRetVal是否在任何地方声明过
function lastCheckInv()
{
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_txtInvNumber").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_txtInvNumber").focus();
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_AddCharges").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
return confirm("Are you sure the information associated with this invoice is correct?");
}
如果没有,var bRetVal=确认。。。。是告诉jscript它是一个变量的友好方式
function lastCheckInv()
{
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_txtInvNumber").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_txtInvNumber").focus();
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_AddCharges").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
return confirm("Are you sure the information associated with this invoice is correct?");
}
函数的前两行是错误的,$位于ASP.Net控件的唯一ID中
在客户端,您必须使用ClientID,将$
如果您确定控件存在,则
上面的代码可能有用
函数的前两行是错误的,$位于ASP.Net控件的唯一ID中
在客户端,您必须使用ClientID,将$
如果您确定控件存在,则
上面的代码可能有用。谢谢您的建议。我肯定没有写那些硬编码的垃圾,我只是在开发一个不充满邪恶的新版本时,被困在试图修复这个问题上。谢谢你的建议。我肯定没有写那些硬编码的垃圾,我只是在开发一个不充满邪恶的新版本时被困在试图修复这个问题。如果你被IE困住了,你可以使用DebugBar如果你被IE困住了,你可以使用DebugBar你如何调用lastCheckInv?除了疯狂的元素ID,我认为这与C无关,net或asp.net。如何调用lastCheckInv?除了疯狂的元素ID,我认为这与C、.net或asp.net无关。