简体   繁体   English

如何编写允许非可编辑文本框的JavaScript函数?

[英]How do write a JavaScript function that allows for non-editable textbox?

I need to have an input textbox in which I can click and the cursor starts blinking but the user cannot change any text inside it. 我需要有一个输入文本框,可以单击它,光标开始闪烁,但用户不能更改其中的任何文本。

I tried using "readonly" or "disabled" attributes, but they do not allow the cursor to be inside the textbox. 我尝试使用“只读”或“禁用”属性,但它们不允许光标位于文本框中。 So, I was thinking of a solution to implement in JavaScript on a normal textbox. 因此,我正在考虑一种在普通文本框中使用JavaScript实施的解决方案。 Is there a plugin that already do this? 是否有已经执行此操作的插件? How do I implement this? 我该如何实施?

EDIT: Thanks for all the answers, but I wanted to make the textarea/input type text as uneditable but selectable at the same time. 编辑:感谢所有的答案,但我想使textarea /输入类型的文本不可编辑,但同时可以选择。 Pressing Ctrl + A inside the textbox doesn't work. 在文本框中按Ctrl + A不起作用。 Is it possible to get the changed value and the old value and compare them and then return false if the values are different, but in all other cases return true so that the Ctrl + A , Shift + end , etc. combinations work? 是否可以获取更改后的值和旧值并进行比较,如果值不同则返回false,但在所有其他情况下返回true,以便Ctrl + AShift + end等组合起作用?

Something like this: 像这样:

<textarea onkeydown="javascript:return false;"></textarea>

would do the trick. 会成功的 ( jsfiddle ) jsfiddle

You can also do that at runtime if you want to: 您还可以在运行时执行以下操作:

<textarea class="readonly"></textarea>

with

$(".readonly").keydown(function() false);

The onkeydown callback captures keystroke events and cancels them using return false; onkeydown回调捕获按键事件并使用return false;取消它们return false; .

Depending on what you are trying to do, you may want to prevent other kind of events, since it is still possible to change the contents with the mouse, for instance. 根据您要尝试执行的操作,您可能希望防止发生其他类型的事件,因为例如仍然可以使用鼠标更改内容。

Your callback function can accept or cancel events depending of the kind of keystroke. 您的回调函数可以接受或取消事件,具体取决于按键的类型。 For example, to enable only ctrl-a and ctrl-c (with jQuery): 例如,仅启用ctrl-a和ctrl-c(使用jQuery):

function keydown(e) {
  if(!e.ctrlKey) return false; // Cancel non ctrl-modified keystrokes
  if(e.keyCode == 65) return true;// 65 = 'a'
  if(e.keyCode == 67) return true;// 67 = 'c'
  return false;
}
$(function(){
  $("inputSelector").keydown(function(){ return false; });
});

You cannot rely on disabling a <textarea> as a user input, as it's trivial to remove any sort of HTML or javascript disabling with firebug, or other tools. 您不能依赖于禁用<textarea>作为用户输入,因为删除任何类型的使用Firebug或其他工具禁用的HTML或javascript都很简单。 Remember that forms aren't limited to the fields you give them, anyone can submit any data to a page. 请记住,表单不仅限于您提供的字段,任何人都可以将任何数据提交到页面。

Disabled inputs are not submitted with a form anyway, bearing that in mind my advice would be to not use a textarea and just print it out. 无论如何,禁用的输入都不会以任何形式提交,请记住,我的建议是不要使用文本区域,而只是将其打印出来。

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

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