keyListener"输入"不起作用

时间:2018-03-17 17:59:48

标签: javascript keyboard-events

function checkId() {
  var password = document.getElementById("passwordBox").value;

  if (password == "superx") {
    return true;
  };

  alert("Not Allowed :(");

  return false;
};

function keyAdd() {
  passwordBox.addEventListener("keypress", function(event) {
    if (event.keyCode == 13) {
      checkId();
    }
  })
};

第二个功能中的问题是什么?

2 个答案:

答案 0 :(得分:0)

1)https://www.w3schools.com/Jsref/event_onkeypress.asp说:" 注意: addEventListener()方法支持在Internet Explorer 8及更早版本中。"尝试:" object.onkeypress = function(){myScript}; "代替...

2)也许你不打电话给#34; keyAdd()"在所有......

答案 1 :(得分:0)

你可以尝试一下

<script>
 
function checkId() {

	var password = document.getElementById("passwordBox").value;
	if (password == "superx") {

		return true;

	}
	 
	alert("Not Allowed :(");
	 
	return false;
};



function keyAdd() {

passwordBox.addEventListener("keypress", function(event) {
if(event.keyCode == 13){

    checkId();

    }
})
};

 </script>
 <input type="password" name="passwordBox" id="passwordBox" onkeypress="return keyAdd(event)"/>