【问题标题】:I want to bind keys in jquery我想在 jquery 中绑定键
【发布时间】:2015-08-19 13:27:48
【问题描述】:

我目前正在用 jQuery 解决一个计算器的任务,我遇到了这个问题: 我想让我的计算器的按钮只能用键盘操作:如 - 如果我按下键盘上的数字 8,数字 8 将显示在屏幕上。我设法用符号(0-9 的数字,符号(+-*/,))做到了这一点,但我不知道如何通过键盘从符号触发想要的操作。我怎样才能建立链接。

 $( 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 代码中,数字符号是逗号,但在屏幕上它显示为点,当我尝试添加 /用数字减去数字逗号被跳过,结果是错误的)。 帮忙?

【问题讨论】:

  • 为什么不直接使用输入字段呢?

标签: jquery replace binding triggers comma


【解决方案1】:

也许您没有从输入中读取正确的代码?试试:

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

【讨论】:

  • 我的意思是我想将一个操作与操作butoon绑定:当我按下“=”时,我希望返回最终值,当我按下“+”时,我想要代码要知道它必须添加
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 2011-06-21
  • 2016-07-31
  • 2020-12-25
  • 2011-05-07
相关资源
最近更新 更多