Javascript 通过javascript禁用文本框中的退格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1937927/
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
Disable backspace in textbox via javascript
提问by Andrew
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;" 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.
原来 onKeyDown="javascript: return false;" 确实有效。我不知道为什么它以前不起作用。我尝试使用一个新的文本框,它很好地阻止了退格。很抱歉所有发布答案希望得到一些代表的人。在我将其标记为赏金之后。
My textboxes now (seem) to block ALL keystrokes and also still work with the calendar extender.
我的文本框现在(似乎)可以阻止所有按键操作,并且仍然可以与日历扩展器一起使用。
回答by Gabriel McAdams
ZX12R was close. This is the correct solution:
ZX12R接近了。这是正确的解决方案:
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。
回答by Thomas Bonini
Can't you just use the HTML readonly="readonly"attribute?
你不能只使用 HTMLreadonly="readonly"属性吗?
<input type="text" name="country" value="Norway" readonly="readonly" />
<textarea rows="3" cols="25" readonly="readonly">
It should work! :)
</textarea>
回答by Jeff Siver
How about using a label for the display and a hidden textbox to get the value back to the server?
如何使用显示标签和隐藏文本框将值返回给服务器?
回答by svinto
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. You can do this several ways, one of the easier if you use jQuery is to add a class to the text-boxes eg. cssclass="readonly"in question and $(".readonly").attr("readonly", true);.
您只需要在客户端控制器上应用只读,这样 asp.net 就看不到它并且仍然在回发时读取数据。您可以通过多种方式执行此操作,如果您使用 jQuery,其中一种更简单的方法是向文本框添加一个类,例如。cssclass="readonly"在问题和$(".readonly").attr("readonly", true);。
回答by KP.
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>
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. You can still access the properties you need though.
可能仍然允许数据通过,尽管在代码隐藏中您必须将控件视为 HtmlInputText 或 HtmlGenericControl 与 TextBox。不过,您仍然可以访问所需的属性。
Just a few ideas anyway...
反正只是一些想法......
回答by ZX12R
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 ...
回答by Pawan
ReadOnly attribute does not help. The backspace still is taking your browser to back page even if your text box is read only..
ReadOnly 属性没有帮助。即使您的文本框是只读的,退格键仍然会将您的浏览器带回页面。
回答by user689967
use regular text boxes not read-only and not Disabled, just use client-side JavaScript to ignore keypresses. This will ignore all keypress so you will have your READONLY behaviour and it will also ignore the backspace.
使用非只读和非禁用的常规文本框,只需使用客户端 JavaScript 忽略按键。这将忽略所有按键,因此您将拥有 READONLY 行为,并且还将忽略退格键。
<input type="text" value="Your Text Here" onkeydown="return false;" />
回答by sankeats
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;" />
回答by dah97765
I was able to do something similar, with Jquery. Just putting it out here for reference!
我可以用 Jquery 做类似的事情。放在这里仅供参考!
$("#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 菜鸟,所以请随时指出它的优点/缺点。

