JavaScript 从 onkeypress 处理程序“返回 false”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7774116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:16:15  来源:igfitidea点击:

JavaScript "return false" from onkeypress handler

javascript

提问by Mad coder.

I used code in the below method to block my keyboard and it worked for me.

我使用以下方法中的代码来阻止我的键盘,它对我有用。

<asp:textbox id="t1" onkeypress="return false;" />

Now I want to add some more for for it and I tired to do the same using extra code as

现在我想为它添加更多,我厌倦了使用额外的代码来做同样的事情

<script type="text/javascript">
fuction disablekeys()
{
return false;
}
</script>
<asp:textbox id="t1" onkeypress="disablekeys();" />

But this code is not working. Why?

但是这段代码不起作用。为什么?

回答by Wayne

You need to return the value returned by disablekeys:

您需要返回由 返回的值disablekeys

<asp:textbox id="t1" onkeypress="return disablekeys();" />

Your new onkeypresshandler currently ignores this value.

您的新onkeypress处理程序当前忽略此值。