本文介绍了禁用输入字段中的某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要使用户类型只有1个,这个类型只有一个, 2,3,4,5,N,O,A,B,C。所有其他角色都应该受到限制。 我可以自己选择受限制/允许的字符作为输入吗? 试试看javascript不太熟悉。这 $(function(){$('#txt'))。 < script src =https://ajax.googleapis.com/ajax/libs/的jquery / 2.1.1 / jquery.min.js>& lt; / script>< input type ='text'id ='txt'value =''onpaste =return false/> data-lang =jsdata-hide =falsedata-console =truedata-babel =false> $(function(){$('#txt')。keypress(function(e){// char char 1,2,3,4,5, N,O,A,B,C let allow_char = [97,98,99,110,111,65,66,67,78,79,49,50,51,52,53]; if(allow_char.indexOf(e.which)!== -1){//做某事} else {return false; < script src =}});}); I have found some instructions similar, but none helps, all of them are either for special chars or for digits.I need to make user type only 1 , 2 , 3, 4, 5, N, O, A, B, C . all other characters should be restricted.Any way I can make my own selection of restricted/allowed chars for inputs ?Not very familiar with javascript. 解决方案 Try this$(function(){ $('#txt').keypress(function(e){ if(e.which == 97 || e.which == 98 || e.which == 99 || e.which == 110 || e.which == 111 || e.which == 65 || e.which == 66 || e.which == 67 || e.which == 78 || e.which == 79 || e.which == 49 || e.which == 50 || e.which == 51 || e.which == 52 || e.which == 53){ } else { return false; } });});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type='text' id='txt' value='' onpaste="return false" />Update 9th March 2018$(function(){ $('#txt').keypress(function(e){ // allowed char: 1 , 2 , 3, 4, 5, N, O, A, B, C let allow_char = [97,98,99,110,111,65,66,67,78,79,49,50,51,52,53]; if(allow_char.indexOf(e.which) !== -1 ){ //do something } else{ return false; } });});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type='text' id='txt' value='' onpaste="return false" /> 这篇关于禁用输入字段中的某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 04:18