2015-06-04 16 views
2

我目前正在解決jQuery中的計算器任務,我有這個問題: 我想讓我的計算器的按鈕只能用鍵盤操作:如 - 如果我按下鍵盤上的數字8將顯示在屏幕上。我設法用符號(數字0-9,符號(+ - * /,))做到這一點,但我不知道如何通過鍵盤從符號中觸發想要的動作。我如何建立鏈接。我想在jQuery中綁定鍵

$(document).ready(function() {  
      var $calc = $("#calculator"); 
      var $display = $("#display"); 
      var $numbers = $("#numbers button"); 
      var $operators = $("#operators button"); 
      var $colspan = $(".colspan"); 

      var init = 0; 
      var operand = init; 
      var operation = null; 
      var afterOperation = false; 

      var reset = function() { 
       $display.text(init); 
       operand = null; 
       operation = null; 
      } 

      $numbers.not($('#colspan')).click(function(event) { 
       event.preventDefault(); 
       var currValue = parseFloat($display.text()); 
       if(afterOperation == false) { 
        if($display.text() == init && $display.text().indexOf(',')<0) { 
         $display.text($(this).text()); 
        } else { 
        $display.append($(this).text()); 
        } 
       } else { 
        $display.text($(this).text()); 
        afterOperation = false; 
       } 
      }); 
      $(window).keypress(function(event) { 
         switch (event.key) { 
         case "1": 
           $display.append("1"); 
           break; 
         case "2": 
           $display.append("2"); 
           break; 
         case "3": 
           $display.append("3"); 
           break; 
         case "4": 
           $display.append("4"); 
           break; 
         case "5": 
           $display.append("5"); 
           break; 
         case "6": 
           $display.append("6"); 
           break; 
         case "7": 
           $display.append("7"); 
           break; 
         case "8": 
           $display.append("8"); 
           break; 
         case "9": 
           $display.append("9"); 
           break; 
         case "0": 
           $display.append("0"); 
           break; 
         case "+": 
           $display.append("+"); 
           break; 
                case "-": 
           $display.append("-"); 
           break;          
         case "/": 
           $display.append("/"); 
           break; 
         case "*": 
           $display.append("*"); 
           break;   
         case ",": 
           $display.append(","); 
           break;  
         case $.ui.keyCode.: 
    console.log("down"); 
    break;          
        } 
      }); 
      $operators.click(function(event) { 
       event.preventDefault(); 
       var currValue = $(this).text(); 
       var currOperand = parseFloat($display.text()); 
       if(currValue != 'CE') { 
       if(operation != null && !afterOperation){ 
        switch(operation) { 
         case '+': 
          $display.text(operand + currOperand); 
          break; 
         case '-': 
          $display.text(operand - currOperand); 
          break; 
         case '*': 
          $display.text(operand * currOperand); 
          break; 
         case '/': 
          if(currOperand != 0) { 
           $display.text(Number(operand/currOperand).toFixed(1)); 
          } else { 
           alert("ERROR"); 
           reset(); 
          } 
          break; 
         } 
        } 
       } 
       if(currValue != '=') { 
         operation = currValue; 
       } else { 
         operation = null; 
         afterOperation = false; 
         return; 
       } 
       if(currValue != 'C') { 
        operation = currValue; 
       } else { 
        $display.text(init); 
       } 
       if(currValue != '←') { 
        operation = currValue; 
       } else { 
        $display.text($display.text().substring(0,$display.text().length-1)); 
       } 
       if(operand = parseFloat($display.text())) { 
         afterOperation = true; 
       } else { 
        reset(); 
       } 

      }); 

此外,我想用逗號替換計算器屏幕上顯示的點號(在我的HTML代碼中,數字的符號是逗號,但在屏幕上它顯示爲點,當我嘗試加/減與數字的逗號跳下數和結果是錯誤的) 幫助

+0

爲什麼不只是使用輸入字段呢? –

回答

0

也許你不會從輸入讀取正確的代碼嘗試:??

$(window).keypress(function(a){ 
     $("#display").append((String.fromCharCode(a.which))); 
}) 
+0

我的意思是我想綁定一個操作與操作butoon:當我按「=」我想最終值被返回,當我按「+」我想要代碼知道它必須做出加成 –