2011-06-09 87 views
0

Hy Guys,Javascript焦點不能在Aspx頁面工作

請看代碼並嘗試幫助。寫入的函數不起作用,但其運行正常。它的目的是把焦點集中在頁面上的下一個內容,在ASPX頁面上使用它。繼承人我下面的代碼:

function SetFocusOnSave(CTag,NTag) 
{ 
    alert('Running'+CTag+NTag); 

    var CurrentTag=document.getElementById(CTag);  
    var NextTag = document.getElementById(NTag); 

    if ((event.keyCode==13)||(event.keyCode==9))  
    {  
     if(CurrentTag.value=="")  
     {  
      alert("Please Enter Detail First");  
      CurrentTag.focus();  
     } 

     if(CurrentTag.value!="") 
     { 
      event.returnValue=true;  
      document.getElementById(NextTag).focus(); 
     }  
    } 
} 

snametxt.Attributes.Add("onkeypress",  
       SetFocusOnSave('<%=snametxt.ClientID%>','<%=sdesctxt.ClientID%>');"); 

回答

1

你試過更換

document.getElementById(NextTag).focus(); 

NextTag.focus(); 

+0

我試過但沒有工作:(! – 2011-06-09 08:14:04

+0

你試過supprime指令「event.returnValue = true;」? – 2011-06-09 13:39:21

0

在驗證中發現錯誤後,您必須添加return false;否則流程將繼續進行直至結束。

試試這個功能:

function SetFocusOnSave(CTag, NTag) { 

    alert('Running' + CTag + NTag); 
    var CurrentTag = document.getElementById(CTag);  
    var NextTag = document.getElementById(NTag); 

    if ((event.keyCode == 13) || (event.keyCode == 9))  
    { 

     if (CurrentTag.value == "")  
     { 

      alert("Please Enter Detail First");  
      CurrentTag.focus(); 
      return false; 
     } 

     if (CurrentTag.value != "") { 
      event.returnValue = true;  
      NextTag.focus(); 
      return false; 
     } 
    } 

}; 
+0

我試過但不工作:(!! – 2011-06-09 08:14:21

+0

它適用於我,請參閱 - http://jsfiddle.net/W9vsu/1/。 我已經通過「if」條件,因爲我無法執行鍵碼13/9。 – 2011-06-09 09:30:17

+0

Im嘗試KeyPress它沒有工作! – 2011-06-09 09:55:56

0

海蘭傢伙伊夫嘗試了新代碼並好在它的工作在這裏很好了我的代碼

function Navigation(CTag, NTag, Number) { 

    var CurrentTag = document.getElementById(CTag); 
    var NextTag = document.getElementById(NTag); 
    var IsNumber = Number; //Checking if value is number 

    if (NextTag.disabled == true) { 
     NextTag.disabled = false; 
     NextTag.className = "txt"; 
    } 

    if (event.keyCode == 9) { 
     CurrentTag.focus(); 
     event.returnvalue = false; 
    } 

    if (event.keyCode != 9) { 
     if (event.keyCode == 13) { 
      if (IsNumber == "Y") { 

       if (NextTag.disabled == true) { 
        NextTag.disabled = false; 
       } 
       if (CurrentTag.value != "") { 
        NextTag.focus(); 
        event.returnvalue = true; 
       } 
       if (CurrentTag.value == "") { 
        alert('Please Enter Value To Proceed Further.'); 
        CurrentTag.focus(); 
        event.returnvalue = false; 
       } 
       if (isNaN(CurrentTag.value)) { 
        alert("Please Enter A Valid Number"); 
        CurrentTag.value = ""; 
        CurrentTag.focus(); 
       } 
      } 
      if (IsNumber == "N") { 
       if (CurrentTag.value == "") { 
        alert('Please Enter Value To Proceed Further.'); 
        CurrentTag.focus(); 
        event.returnvalue = false; 
       } 
       if (CurrentTag.value != "") { 
        NextTag.focus(); 
        event.returnvalue = true; 
       } 
      } 
     } 
    } 
}; 

謝謝你們大家! :)