Javascript 允许文本框中包含某些字符
如何在单个HTML文件中实现这一点?我试图将它添加到标题中,但它不起作用。请有人教我Javascript 允许文本框中包含某些字符,javascript,Javascript,如何在单个HTML文件中实现这一点?我试图将它添加到标题中,但它不起作用。请有人教我 $(“#mytextbox”)。在(“按键”,功能(事件){ //不允许任何与正则表达式模式不匹配的内容(A到Z大写、A到Z小写和空白) //有关JavaScript正则表达式的详细信息,请参见此处:https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions var englishAlphabetAndWhiteS
$(“#mytextbox”)。在(“按键”,功能(事件){
//不允许任何与正则表达式模式不匹配的内容(A到Z大写、A到Z小写和空白)
//有关JavaScript正则表达式的详细信息,请参见此处:https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetAndWhiteSpace=/[A-Za-z]/g;
//从event.which中传递的字符代码中检索密钥
//有关偶数的更多信息,请查看此处:http://stackoverflow.com/q/3050984/114029
var key=String.fromCharCode(event.which);
//警报(事件键码);
//有关键码,请参见此处:http://stackoverflow.com/a/3781360/114029
//keyCode==8是退格
//keyCode==37为左箭头
//keyCode==39是右箭头
//test(key)进行匹配,即根据regex模式测试刚刚键入的密钥
if(event.keyCode==8 | | event.keyCode==37 | | event.keyCode==39 | | | Englishalphabetand Whitespace.test(key)){
返回true;
}
//如果到了这一步,只需返回false,因为键入了不允许的密钥。
返回false;
});
$('#mytextbox')。在(“粘贴”,函数(e)
{
e、 预防默认值();
});代码>
由于您已将代码放入
标记内的
元素中,因此它将在页面加载时运行,并且在DOM准备就绪之前-即此时不存在输入。假设正确引用了jQuery,您可以:
将JS包含到网页中的最佳且最兼容的方法是将其保存到.JS文件中,并将其包含在html页面中,方法是:
如果确实希望它位于同一页面中,请删除src属性并将代码放在脚本标记之间,但可能会出现问题,因为JS代码中有符号,这在xml中是不允许的,因此这将是无效的xhtml
在任何情况下,您拥有的这段代码都使用JQuery库,因此您需要在执行任何操作之前导入它。在你的
因此,您的整个页面应该如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title goes here</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
// code goes here
});
</script>
</head>
<body>
<!-- content of the page goes here -->
</body>
</html>
标题在这里
$(函数(){
//代码在这里
});
以下是如何将其放入单个HTML文件中
你需要
在
等待文档加载完成
$(document).ready(function(){
$("#mytextbox").on("keypress", function(event) {
// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
// For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
// Retrieving the key from the char code passed in event.which
// For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
var key = String.fromCharCode(event.which);
//alert(event.keyCode);
// For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
// keyCode == 8 is backspace
// keyCode == 37 is left arrow
// keyCode == 39 is right arrow
// englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
return true;
}
// If we got this far, just return false because a disallowed key was typed.
return false;
});
$('#mytextbox').on("paste",function(e)
{
e.preventDefault();
});
});
您可以在脚本标记内的正文结束标记之前添加该脚本
<script type="text/javascript">
Your script goes here.
</script>
</body>
你的剧本在这里。
什么不起作用?您是否有
标签中的代码?显示完整的htmlhave您包含了jquery库吗?这是我不能做的。我试着在行乞和行乞结束时把它们放在一边。这是我唯一能做的事情。如果你真的把代码放在
内的
标记中,它将在加载DOM之前运行,因此$(“#mytextbox”)
将找不到任何东西。你就是这样得到它的吗?@Lifestohack
标记在那里无效。
标记。
标记必须位于
之前,而不是之后。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title goes here</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
// code goes here
});
</script>
</head>
<body>
<!-- content of the page goes here -->
</body>
</html>
$(document).ready(function(){
$("#mytextbox").on("keypress", function(event) {
// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
// For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
// Retrieving the key from the char code passed in event.which
// For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
var key = String.fromCharCode(event.which);
//alert(event.keyCode);
// For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
// keyCode == 8 is backspace
// keyCode == 37 is left arrow
// keyCode == 39 is right arrow
// englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
return true;
}
// If we got this far, just return false because a disallowed key was typed.
return false;
});
$('#mytextbox').on("paste",function(e)
{
e.preventDefault();
});
});
<script type="text/javascript">
Your script goes here.
</script>
</body>