简体   繁体   English

通过javascript禁用文本框中的退格

[英]Disable backspace in textbox via javascript

I have a textbox which is extended by an Ajax Control Toolkit calendar.我有一个由 Ajax Control Toolkit 日历扩展的文本框。

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!我已经设法阻止了除退格键之外的所有键!

This is what I have so far:这是我到目前为止:

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

How would I also disable backspace within the textbox using javascript?我将如何使用 javascript 在文本框中禁用退格键?

EDIT编辑

Made an edit since I need a solution in javascript.进行了编辑,因为我需要 javascript 中的解决方案。

EDIT编辑

It turns out that onKeyDown="javascript: return false;"原来 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 was close. ZX12R接近了。 This is the correct solution:这是正确的解决方案:

The TextBox is like this:文本框是这样的:

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

and the script looks like this:脚本如下所示:

<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>

First of all, the backspace wont come through on Key Press, so you have to use Key Down.首先,退格键不会通过 Key Press,所以你必须使用 Key Down。

Can't you just use the HTML readonly="readonly" attribute?你不能只使用 HTML readonly="readonly"属性吗?

<input type="text" name="country" value="Norway"   readonly="readonly" />

<textarea rows="3" cols="25" readonly="readonly">
It should work! :)
</textarea>

如何使用显示标签和隐藏文本框将值返回给服务器?

You need to apply readonly on the client side controller ONLY, so that asp.net doesn't see it and still reads the data on postback.您只需要在客户端控制器上应用只读,这样 asp.net 就看不到它并且仍然在回发时读取数据。 You can do this several ways, one of the easier if you use jQuery is to add a class to the text-boxes eg.您可以通过多种方式执行此操作,如果您使用 jQuery,其中一种更简单的方法是向文本框添加一个类,例如。 cssclass="readonly" in question and $(".readonly").attr("readonly", true); cssclass="readonly"$(".readonly").attr("readonly", true); . .

As others said ReadOnly="True" will break the postback mechanism.正如其他人所说 ReadOnly="True" 会破坏回发机制。

I believe you can get around it in your code-behind by accessing the Request object directly during PageLoad:我相信您可以通过在 PageLoad 期间直接访问 Request 对象在代码隐藏中解决它:

//assuming your textbox ID is 'txtDate'
if(Page.IsPostBack)
{
   this.txtDate.Text = Request[this.txtDate.UniqueID];
}

Your other option is to allow Disabled controls to postback on the form, but this is somewhat of a security concern as readonly fields modified via script could potentially come back:您的另一个选择是允许禁用控件在表单上回发,但这有点安全问题,因为通过脚本修改的只读字段可能会返回:

<form id="MyForm" runat="server" SubmitDisabledControls="True">
..
</form>

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.submitdisabledcontrols.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.submitdisabledcontrols.aspx

I'm not sure the impact of this property on ReadOnly (vs Enabled="False") controls but it's worth trying.我不确定此属性对 ReadOnly (vs Enabled="False") 控件的影响,但值得一试。

And finally - I did run into the same issue you're having a few years ago, and from what I remember there is a difference between using an html input marked as readonly and runat="server", and an actual serverside control where ReadOnly="true".最后 - 我确实遇到了你几年前遇到的同样问题,从我记得的情况来看,使用标记为 readonly 和 runat="server" 的 html 输入和实际的服务器端控件之间存在差异,其中 ReadOnly =“真”。

I have a feeling doing:我有一种感觉:

<input type="text" readonly="readonly" runat="server" id="myTextBox" />

may have still allowed the data to come through, although in the code-behind you have to treat the control as a HtmlInputText or HtmlGenericControl vs. a TextBox.可能仍然允许数据通过,尽管在代码隐藏中您必须将控件视为 HtmlInputText 或 HtmlGenericControl 与 TextBox。 You can still access the properties you need though.不过,您仍然可以访问所需的属性。

Just a few ideas anyway...反正只是一些想法......

I was able to do something similar, with Jquery.我可以用 Jquery 做类似的事情。 Just putting it out here for reference!放在这里仅供参考!

$("#inputID").keypress(function (e)
{e.preventDefault();});

$("#inputID").keydown(function (e)
{e.preventDefault();});

the first prevents keypresses, while the second prevents key down events.第一个防止按键,而第二个防止按键事件。

Still a JS noob, so feel free to point out good / bad things with this.仍然是一个 JS 菜鸟,所以请随时指出它的好 / 坏处。

here is a possible solution... add an event listener...这是一个可能的解决方案...添加一个事件侦听器...

<asp:TextBox ID="TextBox1" runat="server" onKeyPress="KeyCheck;" />

and then the function can be like this..然后功能可以是这样的..

function KeyCheck(e) {
 var KeyID = (window.event) ? event.keyCode : e.keyCode;
 if (KeyID == 8 ) {
    alert('no backspaces!!);
 }
}

doubt if it has to be onkeypress or onkeyup...怀疑它是否必须是 onkeypress 或 onkeyup ...

ReadOnly attribute does not help. ReadOnly 属性没有帮助。 The backspace still is taking your browser to back page even if your text box is read only..即使您的文本框是只读的,退格键仍然会将您的浏览器带回页面。

use regular text boxes not read-only and not Disabled, just use client-side JavaScript to ignore keypresses.使用非只读和非禁用的常规文本框,只需使用客户端 JavaScript 忽略按键。 This will ignore all keypress so you will have your READONLY behaviour and it will also ignore the backspace.这将忽略所有按键,因此您将拥有 READONLY 行为,并且还将忽略退格键。

<input type="text" value="Your Text Here" onkeydown="return false;" />

No Need to call any function and all just try this:无需调用任何函数,只需尝试以下操作:

<asp:TextBox runat="server" ID="txt" onkeydown="return false;" 
    onpaste = "return false;" onkeypress="return false;" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM