简体   繁体   English

为什么此JavaScript不显示确认框?

[英]Why doesn't this JavaScript display a confirm box?

I am trying to fix some bugs in a product I inherited, and I have this snippet of javascript that is supposed to hilight a couple of boxes and pop up a confirm box. 我正在尝试修复继承的产品中的一些错误,并且我使用了这段JavaScript代码段,该代码段可以突出显示几个框并弹出一个确认框。 What currently happens is I see the boxes change color and there is a 5 or so second delay, then it's as if the missing confirm just accepts itself. 目前发生的情况是,我看到包装盒的颜色发生了变化,并且有5秒左右的延迟,这就像丢失的确认仅接受了自己。 Does anyone smarter than I see anything amiss in this code? 在我的代码中,有没有人比我更聪明?

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;

}

The only thing I can think of is if one of the lines before the confirm is throwing an exception and you're never actually getting to the confirm. 我唯一能想到的是,确认之前的某行是否抛出异常,而您实际上从未真正进入过确认。

If you're using IE, make sure script debugging is enabled. 如果您使用的是IE,请确保已启用脚本调试。 If you're using Firefox, install the Firebug add-on and enable it for your website. 如果您使用的是Firefox,请安装Firebug插件并为您的网站启用它。

Or, for very primitive debugging, just put alerts after each statement and figure out where it's bombing. 或者,对于非常原始的调试,只需在每个语句之后放置警报,然后找出轰炸的位置。

You should use the following method to reference your controls from JavaScript: 您应该使用以下方法从JavaScript引用控件:

document.getElementById(<%= txtInvNumber.ClientID %>).style.background = "yellow"

If that doesn't help, try setting a breakpoint in your JavaScript and stepping through it to see where it's failing. 如果这样做没有帮助,请尝试在JavaScript中设置一个断点,并逐步执行该断点以查看失败的地方。

This test script ran fine for me in IE, Firefox, and Opera. 此测试脚本在IE,Firefox和Opera中对我来说运行良好。 So there doesn't seem to be anything wrong with your basic syntax. 因此,您的基本语法似乎没有任何问题。 The problem is either in the ID's (which doesn't fit with the fact that it acts as if confirmed after 5 seconds) or in some other conflicting JavaScript on the page. 问题可能出在ID的问题上(这与5秒钟后好像被确认的事实不符),或者是页面上其他冲突的JavaScript。 It will be difficult to help you without seeing more of the page. 如果不查看更多页面,将很难为您提供帮助。

<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>

A few thoughts: Could be the .focus() call is hiding your confirm behind the page? 有几点想法: .focus()调用.focus()您的确认隐藏在页面后面吗? Or could it be that one of your control id's is not correct causing, the .style.background references to fail? 或者是您的控件ID之一不正确导致.style.background引用失败?

  1. You should set the focus after showing the confirm box, otherwise the confirm box will grab focus away, making that line meaningless. 您应该显示确认框设置焦点,否则确认框将抢走焦点,使该行毫无意义。
  2. Don't hard-code ASP.Net id's like that. 不要像这样硬编码ASP.Net id。 While they typically are consistent, a future version of ASP.net won't guarantee the same scheme, meaning your setting yourself up for pain when it comes time to update this code at some point in the future. 尽管它们通常是一致的,但是ASP.net的未来版本将无法保证采用相同的方案,这意味着您将来在某个时候更新此代码时会感到痛苦。 Use the server-side .ClientID property for the controls to write those values into the javascript somewhere as variables that you can reference. 使用服务器端.ClientID属性作为控件,将这些值作为您可以引用的变量写入javascript。

Update: 更新:
Based on your comment to another response, this code will result in a postback if the function returns true. 根据您对另一个响应的评论,如果函数返回true,则此代码将导致回发。 In that case, there's not point in running the .focus() line at all unless you are going to return false. 在这种情况下,除非您要返回false,否则根本不会运行.focus()行。

I do not like accesing objects directly by 我不喜欢直接访问对象

document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow"; 

If the object is not returned JavaScript will error out. 如果未返回该对象,则JavaScript将出错。 I prefer the method of 我更喜欢

var obj = document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight");

if(obj)
{
    obj.style.background = "yellow";
}

My guess is you are trying to access an object that is not on the DOM, so it never gets to the confirm call. 我的猜测是您正在尝试访问不在DOM上的对象,因此它永远不会到达确认调用。

It might be worth checking that the elements actually exist. 可能值得检查元素是否确实存在。 Also,try delaying the focus until after the confirm(): 另外,尝试将焦点延迟到confirm()之后:

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;

}

Is bRetVal actually declared anywhere? bRetVal实际上在任何地方声明吗?

If not, " var bRetVal = confirm ...." is a friendly way of telling jscript that it is a variable. 如果不是,则“ var bRetVal = Confirm ....”是一种告诉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?");    
}

The first two lines in your function are wrong, $ are in the UniqueID of an ASP.Net Control. 您的函数的前两行是错误的,$在ASP.Net控件的UniqueID中。

On Clientside you have to use the ClientID, replace $ with _ . 在Clientside上,您必须使用ClientID,将$替换为_。

If you are sure that the Controls exists, the code above might work. 如果您确定控件存在,则上面的代码可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM