通过JavaScript在文本框中禁用退格 [英] Disable backspace in textbox via javascript

查看:142
本文介绍了通过JavaScript在文本框中禁用退格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Ajax控件工具包日历扩展的文本框。

I have a textbox which is extended by an Ajax Control Toolkit calendar.

我想使它使得用户不能编辑文本框和将要代替使用日历扩展器输入

I want to make it so that the user cannot edit the textbox and will have to instead use the calendar extender for input.

我设法阻止除退格键全部!

I have managed to block all keys except backspace!

这是我迄今为止:

<asp:TextBox ID="TextBox1" runat="server" onKeyPress="javascript: return false;" onKeyDown="javascript: return false;" onPaste="javascript: return false;" />

我怎么会还使用javascript文本框内禁用退格?

How would I also disable backspace within the textbox using javascript?

修改

做了编辑,因为我需要在JavaScript的解决方案。

Made an edit since I need a solution in javascript.

修改

原来,的onkeydown =JavaScript的:返回false;确实工作。我不知道为什么它不工作之前。我试图用一个新的文本框,并阻止退格罚款。很抱歉给大家谁张贴的答案希望能得到一些代表ESP。之后,我标志着它赏金。

It turns out that onKeyDown="javascript: return false;" DOES work. I have no idea why it wasn't working before. I tried using a new textbox and it blocked backspaces fine. So sorry to everyone who posted an answer hoping to get some rep esp. after I marked it for bounty.

我现在的文本框(似乎)以阻止所有的按键,也仍然与日历扩展工作。

My textboxes now (seem) to block ALL keystrokes and also still work with the calendar extender.

推荐答案

ZX12R接近。这是正确的解决办法:

ZX12R was close. This is the correct solution:

文本框是这样的:

    <asp:TextBox ID="TextBox1" runat="server" onKeyDown="preventBackspace();"></asp:TextBox>

和脚本如下:

<script type="text/javascript">
    function preventBackspace(e) {
        var evt = e || window.event;
        if (evt) {
            var keyCode = evt.charCode || evt.keyCode;
            if (keyCode === 8) {
                if (evt.preventDefault) {
                    evt.preventDefault();
                } else {
                    evt.returnValue = false;
                }
            }
        }
    }
</script>

首先,退格不会对重点preSS才能通过,所以你必须使用Key向下。

First of all, the backspace wont come through on Key Press, so you have to use Key Down.

这篇关于通过JavaScript在文本框中禁用退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆