键侦听器"Enter"不起作用


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();
    }
  })
};

第二个函数有什么问题?

1( https://www.w3schools.com/Jsref/event_onkeypress.asp 说:">注意:Internet Explorer 8 和更早版本不支持 addEventListener(( 方法。

2(也许你根本不叫"keyAdd(("...

你可以试试

<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)"/>

最新更新