是否有任何理由不能将所需的console.log语句打印到我的控制台&允许非法字符(即任何不是数字0-9的字符)记录到控制台?现在,我按下每个按钮都会记录到控制台,即使是我不想要的按钮,也不是我想要的console.log语句。这段代码的行为如此不一致我质疑我的IDE或Chrome是否有问题。注意,我也使用过keyup并遇到了同样的问题。
$(document).ready(function () {
// declare empty infix string
// declare empty infix string
let infix = "";
// get input from user on key press (separate function)
(function getInput() {
$(window).keypress(function(e) {
if("1234567890".includes(e.key)) {
infix += e.key;
console.log("infix" + e.key);
}
});
})();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body {
background-color: #000000;
}
.container {
width: 420px;
height: 500px;
background-color: #c8c8c8;
}
.btn-group {
margin: 5px;
}
.btn.btn-primary {
width: 59px;
height: 34px;
}
#title {
background-color: #c8c8c8;
}
#screen {
width: 300px;
height: 35px;
border-radius: 15px;
margin: 5px 0px 10px 0px;
/* take care of cursor's right alignment */
color:black;
font-size: 15px;
text-align:right;
padding-bottom:0;
font-family: helvetica;
}
</style>
<script>
$(document).ready(function () {
// declare empty infix string
// declare empty infix string
let infix = "";
// get input from user on key press (separate function)
(function getInput() {
$(window).on("keyup", function(e) {
if("1234567890".includes(e.key)) {
infix += e.key;
console.log("infix" + e.key);
}
});
})();
// It's IIFE, not jQuery
// display input on screen (separate function)
// assign input to string (separate function)
// get operator from user and add to infix
// if equals pressed
// eval infix string
// if clear pressed
// reset to empty string
// display blank screen
});
</script>
<title>Calculator</title>
</head>
<body>
<div class="container">
<div class="row text-center">
<div id="title" class="col-md-12 text-center">
<h4>Business Analyst</h4>
</div>
<div class="col-md-12 body text-center">
<input id="screen" type=text>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="enter" class="btn btn-primary">Enter</button>
</div>
<div class="btn-group">
<button id="compute" class="btn btn-primary">CPT</button>
</div>
<div class="btn-group">
<button id="clear-cash-flow" class="btn btn-primary">CCF</button>
</div>
<div class="btn-group">
<button id="analyst-mode" class="btn btn-primary">AM</button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="fut-val" class="btn btn-primary">FV</button>
</div>
<div class="btn-group">
<button id="cash-flow" class="btn btn-primary">CF</button>
</div>
<div class="btn-group">
<button id="net-pv" class="btn btn-primary">NPV</button>
</div>
<div class="btn-group">
<button id="irr" class="btn btn-primary">IRR</button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="period" class="btn btn-primary">N</button>
</div>
<div class="btn-group">
<button id="int-rate" class="btn btn-primary">I|Y</button>
</div>
<div class="btn-group">
<button id="pres-val" class="btn btn-primary">PV</button>
</div>
<div class="btn-group">
<button id="payment" class="btn btn-primary">PMT</button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="clear" class="btn btn-primary">C</button>
</div>
<div class="btn-group">
<button id="percent" class="btn btn-primary">%</button>
</div>
<div class="btn-group">
<button id="lftparen" class="btn btn-primary">(</button>
</div>
<div class="btn-group">
<button id="rtparen" class="btn btn-primary">)</button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="seven" class="btn btn-primary">7</button>
</div>
<div class="btn-group">
<button id="eight" class="btn btn-primary">8</button>
</div>
<div class="btn-group">
<button id="nine" class="btn btn-primary">9</button>
</div>
<div class="btn-group">
<button id="division" class="btn btn-primary"><span>÷</span></button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="four" class="btn btn-primary">4</button>
</div>
<div class="btn-group">
<button id="five" class="btn btn-primary">5</button>
</div>
<div class="btn-group">
<button id="six" class="btn btn-primary">6</button>
</div>
<div class="btn-group">
<button id="division" class="btn btn-primary">X</button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="one" class="btn btn-primary">1</button>
</div>
<div class="btn-group">
<button id="two" class="btn btn-primary">2</button>
</div>
<div class="btn-group">
<button id="three" class="btn btn-primary">3</button>
</div>
<div class="btn-group">
<button id="subtraction" class="btn btn-primary">-</button>
</div>
</div>
<div class="col-md-12 body text-center">
<div class="btn-group">
<button id="zero" class="btn btn-primary">0</button>
</div>
<div class="btn-group">
<button id="decimal" class="btn btn-primary">.</button>
</div>
<div class="btn-group">
<button id="equal" class="btn btn-primary">=</button>
</div>
<div class="btn-group">
<button id="addition" class="btn btn-primary">+</button>
</div>
</div>
</div>
</div>
</body>
</html>
&#13;