jQuery 清除文本区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8284960/
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
Clear text area
提问by vinanghinguyen
In Onselect event I have script:
在 Onselect 事件中,我有脚本:
$("#vinanghinguyen_images_bbocde").val('');
$("#vinanghinguyen_images_bbocde").val(vinanghinguyen_final_bbcode);
I want clear text area id="vinanghinguyen_images_bbocde" before add value to it. but textarea add add add add and value and not clear. I want clear it before add value
在向其添加值之前,我需要清除文本区域 id="vinanghinguyen_images_bbocde"。但是 textarea add add add add add 和 value 并不清楚。我想在增加价值之前清除它
I use uploadify here is my function
我在这里使用uploadify是我的功能
<script type = "text/javascript" >
$(document).ready(function() {
vinanghinguyen_bbcode = '';
vinanghinguyen_final_bbcode = '';
vinanghinguyen_link = '';
vinanghinguyen_final_derect_link = '';
response = '';
$('#file_upload').uploadify({
'uploader' : '{SITE_FULL_URL}/uploadify/uploadify.swf',
'script' : '{SITE_FULL_URL}/uploadify/uploadify.php',
'cancelImg' : '{SITE_FULL_URL}/uploadify/cancel.png',
'folder' : 'data/picture_upload/2011',
'auto' : false,
'multi' : true,
'buttonText': '',
'onComplete': function(event, ID, fileObj, response, data) {
vinanghinguyen_bbcode = '[IMG]' + 'http://cnttvnn.com' + response + '[/IMG]' + '\n';
vinanghinguyen_final_bbcode = vinanghinguyen_final_bbcode + vinanghinguyen_bbcode;
vinanghinguyen_derect_link = 'http://cnttvnn.com' + response + '\n';
vinanghinguyen_final_derect_link = vinanghinguyen_final_derect_link + vinanghinguyen_derect_link;
$("#vinanghinguyen_images_bbocde").val('').val(vinanghinguyen_final_bbcode);
//$("#vinanghinguyen_images_derect_link").val(vinanghinguyen_final_derect_link);
$("#vinanghinguyen_result").show();
$(".uploadifyQueue").height(5);
},
'onSelect': function(event, ID, fileObj) {
$("#vinanghinguyen_images_bbocde").val('');
$("#vinanghinguyen_result").hide();
$(".uploadifyQueue").height(315);
},
});
});
</script>
回答by Jakub Arnold
When you do $("#vinanghinguyen_images_bbocde").val('');
, it removes all the content of the textarea, so if that's not what is happening, the problem is probably somewhere else.
当你这样做时$("#vinanghinguyen_images_bbocde").val('');
,它会删除 textarea 的所有内容,所以如果这不是正在发生的事情,那么问题可能出在其他地方。
It might help if you post a little bit larger portion of your code, since the example you provided works.
如果您发布更多的代码可能会有所帮助,因为您提供的示例有效。
回答by Karl Wenzel
Use $('textarea').val('')
.
使用$('textarea').val('')
.
The problem with using
$('textarea').text('')
, or
$('textarea').html('')
for that matter is that it will only erase what was in the original DOM sent by the server. If a user clears it and then enters new input, the clear button will no longer work. Using .val('')
handles the user input case properly.
使用
$('textarea').text('')
, 或者
$('textarea').html('')
就此而言的问题在于,它只会删除服务器发送的原始 DOM 中的内容。如果用户清除它然后输入新的输入,清除按钮将不再起作用。Using.val('')
正确处理用户输入的情况。
回答by mzonerz
This works:
这有效:
$('#textareaName').val('');
回答by mzonerz
This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element.
此方法不仅删除子(和其他后代)元素,还删除匹配元素集中的任何文本。这是因为,根据 DOM 规范,元素内的任何文本字符串都被视为该元素的子节点。
$('textarea').empty()
回答by confucius
try this
尝试这个
$("#vinanghinguyen_images_bbocde").attr("value", "");
回答by Achu S
Try this,
尝试这个,
$('textarea#textarea_id').val(" ");
回答by duindain
I just tried using this code and @psynnott's answer was correct though I needed to know it would work repeatedly, seems to work with jquery 1.7.1 >
我只是尝试使用此代码,@psynnott 的答案是正确的,但我需要知道它会重复工作,似乎适用于 jquery 1.7.1 >
I modified the jfiddle to the following http://jsfiddle.net/Rjj9v/109/
我将 jfiddle 修改为以下 http://jsfiddle.net/Rjj9v/109/
$('#mytext').text('');
This is not a new answer @psynnott is correct I am just providing a more concise example that shows the textarea is still working after the clear because if you use .val("") the text area stops working
这不是一个新答案@psynnott 是正确的我只是提供了一个更简洁的示例,显示文本区域在清除后仍在工作,因为如果您使用 .val("") 文本区域停止工作
回答by user1920925
Correct answer is: $("#selElement_Id option:selected").removeAttr("selected");
正确答案是: $("#selElement_Id option:selected").removeAttr("selected");
回答by Akash Kumar
Rather simpler method would be by using JavaScript method of innerHTML.
更简单的方法是使用innerHTML 的JavaScript 方法。
document.getElementById("#id_goes_here").innerHTML = "";
Rather simpler and more effective way.
比较简单有效的方法。
回答by Neo
I agree with @Jakub Arnold's answer. The problem should be somewhere else. I could not figure out the problem but found a work around.
我同意@Jakub Arnold 的回答。问题应该在其他地方。我无法弄清楚问题,但找到了解决方法。
Wrap your concerned element with a parent element and cause its html to create a new element with the id you are concerned with. See below
用一个父元素包装你关心的元素,并使其 html 使用你关心的 id 创建一个新元素。见下文
<div id="theParent">
<div id="vinanghinguyen_images_bbocde"></div>
</div>
'onSelect' : function(event,ID,fileObj) {
$("#theParent").html("<div id='vinanghinguyen_images_bbocde'></div>");
$("#vinanghinguyen_result").hide();
$(".uploadifyQueue").height(315);
}