javascript - 输入字段不能输入其他字符
问题描述
我可以知道是什么错误导致我的输入字段无法输入字符吗?因为在输入字段中设置></\":*?|
了这些符号限制后,其他字符无法在输入字段中输入。
<!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>
希望有人能帮我解决这个问题。谢谢。
解决方案
只需删除 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) {
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>
推荐阅读
- java - Java String DateTime 到 toEpochMilli - 不同的区域
- amazon-web-services - 如何在不影响数据库性能的情况下处理流量峰值
- view-scope - 查看参数和@ViewScoped bean:服务器重启后恢复
- django - 无法在 Django 中使用 RadioSelect 按钮来获取输出
- wordpress - 想要在 WordPress Infinite Scroll - Ajax Load More 插件中按总帖子显示当前帖子编号
- sql - 识别 SQL Server 2014 中的行模式
- javascript - 如何在 React 中动态更改状态?
- fortran - 大家好,我不能用“Script”或“Atom runner”运行fortran代码。如何在 ATOM 上运行 fortran?
- excel - 修改 VBA 代码以分别更改每行的单元格输入
- node.js - 使用节点将文件内容流式传输到标准输出