猿问

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

我可以知道是什么错误导致我的输入字段无法输入字符吗?因为在输入字段中设置></\":*?|了这些符号限制后,其他字符无法在输入字段中输入。


<!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>

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



绝地无双
浏览 110回答 2
2回答

人到中年有点甜

if ("></\":*?|".indexOf(chr) >= 0){&nbsp; showError(chr)&nbsp; &nbsp;return false;}return true // !!!<!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) {&nbsp; var errBox = document.querySelector("#error-box");&nbsp; errBox.textContent = "The character " + key.toString() + " is not allowed!";&nbsp; //Dismiss the error&nbsp; window.setTimeout(function () {&nbsp; &nbsp; &nbsp; errBox.textContent = "";&nbsp; }, 10000)}document.getElementById("function_code").onkeypress = function(e) {var chr = String.fromCharCode(e.which);if ("></\":*?|".indexOf(chr) >= 0){&nbsp; showError(chr)&nbsp; &nbsp;return false;}return true};</script>

隔江千里

只需删除 return false; 或将其更改为 true如果您返回 false,则不会输入任何内容<!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) {&nbsp; var errBox = document.querySelector("#error-box");&nbsp; errBox.textContent = "The character " + key.toString() + " is not allowed!";&nbsp; //Dismiss the error&nbsp; window.setTimeout(function () {&nbsp; &nbsp; &nbsp; errBox.textContent = "";&nbsp; }, 10000)}document.getElementById("function_code").onkeypress = function(e) {var chr = String.fromCharCode(e.which);if ("></\":*?|".indexOf(chr) >= 0)&nbsp; showError(chr)};</script>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答