jQuery - remove user input text from textarea

∥☆過路亽.° 提交于 2019-11-29 06:11:35

问题


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.

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)

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

Thanks in advance.

-Thor


回答1:


You're looking for .val():

$("#commentField").val('');

Example: http://jsfiddle.net/andrewwhitaker/q6eLV/




回答2:


Since textarea is a input field it has a value property so you have to use val() method. Try this

$("#commentField").val('');



回答3:


In jQuery it's actually $("#commentField").val(" ");




回答4:


You can remove text from the textarea like this:

$("#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?.

Using regular javascript with innerHTML it works for me in FF6. demo here




回答5:


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.



来源:https://stackoverflow.com/questions/7109352/jquery-remove-user-input-text-from-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!