onpaste事件在javascript

时间:2017-12-02 07:59:26

标签: javascript html

我正在使用javascript函数追加值....当我使用onkeypress事件时,当斜杠(/)给出时,值会正确附加警告消息.....但当我使用onpaste时事件值没有正确附加到数组....有人可以帮我解决这个问题

function onlyAlphabets(e, t) {
    
	try {
        if (window.event) {
            var charCode = window.event.keyCode;
         
        }
        else if (e) {
            var charCode = e.which;
            
        }
        else { return true; }
       
        if ((charCode == 47)){
        	
        	alert("Please use any other charachter! ");
       return false;
        }
        else{
            return true;
        }
    }
    catch (err) {
        alert(err.Description);
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="table-responsive">
<table class="table table-striped table-bordered table-fixed table-hover table-condensed"  id='+tables+' onkeypress="return onlyAlphabets()" >
<thead>
<tr><th></th><th>Code</th><th>Sub Category</th></tr>
</thead>
<tbody class="tbody"></tbody>
</table>
</div>';

1 个答案:

答案 0 :(得分:0)

像这样添加liName,然后重试。

<body>
function onlyAlphabets(e, t) {
    
	try {
        if (window.event) {
            var charCode = window.event.keyCode;
         
        }
        else if (e) {
            var charCode = e.which;
            
        }
        else { return true; }
       
        if ((charCode == 47)){
        	
        	alert("Please use any other charachter! ");
       return false;
        }
        else{
            return true;
        }
    }
    catch (err) {
        alert(err.Description);
    }
}

如果您将按键事件附加到与 <body onkeypress="return onlyAlphabets()"> <div class="table-responsive"> <table class="table table-striped table-bordered table-fixed table-hover table-condensed" id='+tables+' > <thead> <tr><th></th><th>Code</th><th>Sub Category</th></tr> </thead> <tbody class="tbody"></tbody> </table> </div> </body>不同的元素,则在按下该键时,焦点不在该元素中。
这就是桌子无法识别事件的原因..