提问者:小点点

输入字段不能键入其他字符


请问是什么错误导致我的输入字段无法键入字符? 因为在设置>后,这些符号限制在输入字段中,其他字符不能在输入字段中键入。

null

<!DOCTYPE html>
<html>
<body>

<h1>Can't type character in the input field</h1>


<input type="text" class="form-control blank" id="function_code" name="function_code" title="function_code" onpaste="return false">
<div id="error-box"></div>

</body>
</html>

<script>

function showError (key) {
  var errBox = document.querySelector("#error-box");
  errBox.textContent = "The character " + key.toString() + " is not allowed!";
  //Dismiss the error
  window.setTimeout(function () {
      errBox.textContent = "";
  }, 10000)
}


document.getElementById("function_code").onkeypress = function(e) {
var chr = String.fromCharCode(e.which);

if ("></\":*?|".indexOf(chr) >= 0)
  showError(chr)
  return false;
};
</script>

null

希望有人能帮我解决这个问题。 谢了。


共2个答案

匿名用户

if ("></\":*?|".indexOf(chr) >= 0){
  showError(chr)
   return false;
}
return true // !!!

null

<!DOCTYPE html>
<html>
<body>

<h1>Can't type character in the input field</h1>


<input type="text" class="form-control blank" id="function_code" name="function_code" title="function_code" onpaste="return false">
<div id="error-box"></div>

</body>
</html>

<script>

function showError (key) {
  var errBox = document.querySelector("#error-box");
  errBox.textContent = "The character " + key.toString() + " is not allowed!";
  //Dismiss the error
  window.setTimeout(function () {
      errBox.textContent = "";
  }, 10000)
}


document.getElementById("function_code").onkeypress = function(e) {
var chr = String.fromCharCode(e.which);

if ("></\":*?|".indexOf(chr) >= 0){
  showError(chr)
   return false;
}
return true
};
</script>

匿名用户

只需删除返回的false; 或将其更改为true

如果返回false,则不会键入任何内容

null

<!DOCTYPE html>
<html>
<body>

<h1>Can't type character in the input field</h1>


<input type="text" class="form-control blank" id="function_code" name="function_code" title="function_code" onpaste="return false">
<div id="error-box"></div>

</body>
</html>

<script>

function showError (key) {
  var errBox = document.querySelector("#error-box");
  errBox.textContent = "The character " + key.toString() + " is not allowed!";
  //Dismiss the error
  window.setTimeout(function () {
      errBox.textContent = "";
  }, 10000)
}


document.getElementById("function_code").onkeypress = function(e) {
var chr = String.fromCharCode(e.which);

if ("></\":*?|".indexOf(chr) >= 0)
  showError(chr)
};
</script>