2008-10-07 5 views
1

내가 상속 한 제품의 버그를 수정하려고하는데, 자바 스크립트 스 니펫이 있습니다.이 스 니펫은 두 개의 상자를 표시하고 확인 상자를 팝업합니다. 현재 발생하는 현상은 상자의 색상이 바뀌고 5 초 정도의 지연이있는 것을 볼 수 있습니다. 누락 된 확인 내용이 그대로 적용됩니다. 아무도 내가이 코드에서 불평하는 것을 보는 것보다 더 똑똑한가? 확인하기 전에 라인 중 하나가 예외를 던지고 당신은 결코 실제로 확인에 도착하지한다면 내가 생각할 수있는왜이 JavaScript는 확인 상자를 표시하지 않습니까?

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; 

} 
+0

미친 요소 ID 이외에, 나는 이것이 C#, .net 또는 asp.net과 아무 관련이 없다고 생각하지 않습니다. – Neall

+0

어떻게 lastCheckInv()를 호출합니까? – leppie

답변

7

있는 유일한 방법입니다.

IE를 사용하는 경우 스크립트 디버깅이 설정되어 있는지 확인하십시오. Firefox를 사용하는 경우 Firebug 애드온을 설치하고 웹 사이트에 사용할 수 있도록 설정하십시오.

아주 원시적 인 디버깅을 위해서는 각 명령문 다음에 경고를하고 폭탄이있는 곳을 파악하십시오.

+0

IE가 붙어있는 경우 DebugBar를 사용할 수 있습니다. – StingyJack

1

몇 가지 생각 : .focus() 전화가 페이지 뒤에서 확인을 숨길 수 있습니까? 또는 컨트롤 ID 중 하나가 올바르지 않아서 .style.background 참조가 실패 할 수 있습니까?

  1. 당신은 그렇지 않으면 확인 상자가 줄을 의미하고, 멀리 초점을 사로 잡고, 확인 상자를 보여주는 후 초점 을 설정해야합니다.
  2. ASP.Net ID를 하드 코딩하지 마십시오. ASP.net은 일반적으로 일관성이 있지만 ASP.net의 향후 버전은 동일한 계획을 보장하지 않습니다. 즉, 향후 어느 시점에이 코드를 업데이트해야 할 때 고통을 느끼게 될 것입니다. 컨트롤의 서버 측 .ClientID 속성을 사용하여 참조 할 수있는 변수로 해당 값을 자바 스크립트에 쓸 수 있습니다.

업데이트 : 함수가 true를 돌려주는 경우 다른 응답에 대한 귀하의 의견을 바탕으로
는,이 코드는 다시 게시가 발생합니다. 이 경우 거짓을 반환하지 않는 한 .focus() 행을 전혀 실행하지 않아도됩니다.

+0

조언 해 주셔서 감사합니다. 나는 하드 코딩 된 쓰레기를 쓰지 않았고, 악마로 가득 찬 새로운 버전을 개발하는 동안이 문제를 해결하려고 노력했습니다. –

5

당신은 자바 스크립트에서 컨트롤을 참조하기 위해 다음과 같은 방법을 사용한다 :

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

를이 방법으로 문제가 해결되지 않으면, 자바 스크립트에 중단 점을 설정하고 실패 어디에보고 그것을 통해 스테핑보십시오.

2

이 테스트 스크립트는 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> 
0

나는 객체가 자바 스크립트 오류가 밖으로 것입니다 반환하지 않을 경우

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

직접 객체를 accesing 좋아하지 않는다. 난 당신이 DOM에없는 개체에 액세스하려고

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

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

내 생각의 방법입니다 좋아, 그래서 확인 전화에 도달하지 않습니다.

0

요소가 실제로 존재하는지 확인하는 것이 좋습니다. 또한 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; 

} 
0

bRetVal은 실제로 어디에서나 선언 되었습니까?

그렇지 않으면 "var bRetVal = confirm ...."이 jscript에 변수임을 알려주는 친숙한 방법입니다.

0
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에서 ClientID를 사용해야 할 경우 $를 _로 바꿉니다.

컨트롤이있는 것이 확실하면 위의 코드 이 작동 할 수 있습니다.

관련 문제