2012-06-02 51 views
0

我正在使用jquery在Alt + S上保存表單,同時我也在檢查必填字段。它工作正常,但我有一個問題。當我在輸入必填字段之前按Alt + S時,它會給我提供所需字段的錯誤,並且在我輸入所需字段中的任何內容的時刻,表單將被保存。我想在錯誤發生後填充字段,並且如果用戶再次按下Alt + S,則表單應該被保存。熱鍵jquery

jQuery代碼

$(document).ready(function() { 
    var isAltKey = false; 
    var isShiftKey = false; 
    document.onkeyup = function (e) { 
     if (e.which == 18) isAltKey = false; 
     if (e.which == 16) isShiftKey = false; 
    } 

    document.onkeydown = function (e) { 
     if (e.which == 18) isAltKey = true; 
     if (e.which == 16) isShiftKey = true; 

     //ALT+S 
     if (e.which == 83 && isAltKey == true) { 
      TriggerSaveButton(); 
      StopDefaultAction(e); 
     } 

     else if (e.which == 9 && isShiftKey == true) { 
      if (document.getElementById("cmbUnder_OptionList").focus() == true) { 
       document.getElementById("txtGroupSname").focus(); 
       StopDefaultAction(e); 
      } 
     } 

    } 
    function StopDefaultAction(e) { 
     if (e.preventDefault) { e.preventDefault() } 
     else { e.stop() }; 
     e.returnValue = false; 
     e.stopPropagation(); 
    } 



    function TriggerSaveButton() { 
     var groupname = $('#txtGroupName').val(); 
     if ($('#tab2').is(":visible")) { 
      if ((document.getElementById('<%= txtGroupName.ClientID %>').value == "") && ($('<%= listboxdestination.ClientID %>').children().length == 0)) { 
       ValidatorEnable(document.getElementById('<%= reqtxtGroupName.ClientID %>'), true); 
       ValidatorEnable(document.getElementById('<%= reqlstboxdest.ClientID %>'), true); 
       return false; 
      } 

      else if (groupname != "" && ($("#<%= listboxdestination.ClientID %> option").length != 0)) { 
       javascript: __doPostBack('<%=btnaddfrmSave.UniqueID %>', ''); 

      } 
     } 
     else { 
      if ((document.getElementById('<%= txtGroupName.ClientID %>').value == "")) { 
       ValidatorEnable(document.getElementById('<%= reqtxtGroupName.ClientID %>'), true); 
       return false; 
      } 

      else if (groupname != "") { 
       javascript: __doPostBack('<%=btnaddfrmSave.UniqueID %>', ''); 

      } 

     } 

    } 
});  
+0

可以給我發送一些更多的表單代碼。所以很容易發現問題..? –

回答

0

按我的意見,你應該使用的Asp.net Jquery的表單驗證不Requirefield驗證。

當前您在使用您所需的字段驗證程序時可以將其替換爲Jquery客戶端驗證。 所以它可能會解決你的問題。

請參閱如何驗證使用Jquery。

  1. jquery for validation Plugins
  2. formValidator
+0

你可以告訴我我的代碼請 – asifa

+0

其餘的是asp.net的形式,這是很簡單的與所需的現場驗證 – asifa

+0

嗨,@asifa請顯示您的解決方案的參考鏈接。我添加了兩個引用鏈接到我的答案.. –

0

的代碼:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
     <script>$(function(){ 
     $(document).keyup(function(event){ 
       if(event.which == 65){ 
        alert("The Letter A was pressed"); 
       } 
     }); 
    }); 
    </script> 

每個鍵都有一個數值,例如,a等於65且b爲66(等)。

http://youtu.be/xXZsTNwfMNc 這裏是一個教程,將向您展示如何在jQuery中使用熱鍵。

+0

請注意,[僅限鏈接回答](http://meta.stackoverflow.com/tags/link-只有答案/信息),所以答案應該是搜索解決方案的終點(而另一個引用停留時間往往會隨着時間推移而變得過時)。請考慮在此添加獨立的摘要,並將鏈接保留爲參考。 – kleopatra