jQuery - 从 textarea 中删除用户输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7109352/
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
jQuery - remove user input text from textarea
提问by Thor A. Pedersen
Okay I have a list of devices where i can select which to edit. I have 3 states for the edit. When no devices are selected, when 1 device is selected or when x devices are selected.
好的,我有一个设备列表,我可以在其中选择要编辑的设备。我有 3 个状态进行编辑。未选择设备时、选择了 1 个设备或选择了 x 个设备时。
The problem i have is when a user type some text in the textarea (commentField) and cancels the edit to edit another device, the text there was typed in the textarea won't disapere. It stays so when i get the new dialog for the new edit, the commentfield has the text from the old commentField (as if it wasn't cleared)
我遇到的问题是,当用户在 textarea (commentField) 中键入一些文本并取消编辑以编辑另一个设备时,在 textarea 中键入的文本不会消失。当我获得新编辑的新对话框时,它保持不变,评论字段具有来自旧评论字段的文本(好像它没有被清除)
I have tried the following codes to remove the text (both when then cancel button is pressed and when i start a new dialog), but nothing works:
我尝试了以下代码来删除文本(在按下取消按钮和开始新对话框时),但没有任何效果:
$("#commentField").text(" ");
$("#commentField").value = ' ';
Is there anyone who knows how to remove user-typed text from a textarea using jQuery??
有没有人知道如何使用 jQuery 从 textarea 中删除用户输入的文本?
Thanks in advance.
提前致谢。
-Thor
-雷神
回答by Andrew Whitaker
You're looking for .val()
:
您正在寻找.val()
:
$("#commentField").val('');
回答by ShankarSangoli
Since textarea
is a input field it has a value
property so you have to use val()
method. Try this
由于textarea
是输入字段,因此它具有value
属性,因此您必须使用val()
方法。尝试这个
$("#commentField").val('');
回答by Ali
In jQuery it's actually $("#commentField").val(" ");
在 jQuery 中,它实际上是 $("#commentField").val(" ");
回答by Uku Loskit
You can remove text from the textarea like this:
您可以像这样从 textarea 中删除文本:
$("#commentField").html("");
EDIT: ok, this does not work, but I'd be really interested as to why, and I understand the down-vote. I always thought that the text between the textarea
tags was innerHTML and html()
is supposed to replace just that. Wouldn't it make sense to have textarea like input? or is it just because textareas contain long amounts of text that would look tacky between quotation marks?.
编辑:好的,这行不通,但我真的很想知道为什么,我理解投反对票。我一直认为textarea
标签之间的文本是innerHTML,html()
应该替换它。像输入一样使用 textarea 难道没有意义吗?还是仅仅因为 textareas 包含大量文本,在引号之间看起来很俗气?。
Using regular javascript with innerHTML it works for me in FF6. demo here
在 FF6 中使用带有 innerHTML 的常规 javascript 它对我有用。 演示在这里
回答by Manochehr
1) <textarea name="editor1" id="editor1" rows="10" cols="80"></textarea>
2) CKEDITOR.replace('editor1');
3) initiated = true;
to write your text in you text area
1)$("#editor1").val("your input value");
to clean your text area
1) $("#editor1").reset();
tank you for reading this.
1) <textarea name="editor1" id="editor1" rows="10" cols="80"></textarea>
2) CKEDITOR.replace('editor1');
3)initiated = true;
在你的文本区写你的文本
1)$("#editor1").val("your input value");
清理你的文本区
1)$("#editor1").reset();
让你阅读这篇文章。