[英]How to clear text from textarea input?
I have the following form and I would like to clear any user inputs when I close the form.我有以下表单,我想在关闭表单时清除所有用户输入。 I do so with the following JQ function, however the textarea elements do not clear in the same manner ( #reply
will only clear using .val("")
).我使用以下 JQ 函数执行此操作,但是 textarea 元素不会以相同的方式清除( #reply
只会使用.val("")
清除)。 I do not understand why that is.我不明白为什么会这样。
HTML
<div class="form" id="form">
<form id="postandreply" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" >
<div id="textarea">
<label for="posttopic">Topic</label>
<textarea id="posttopic" name="posttopic" maxlength="100" cols="50" rows="3"></textarea>
<label for="comment">Comment</label>
<textarea id="comment" name="posttext" maxlength="500" cols="80" rows="6"></textarea>
<label for="reply">Reply</label>
<textarea id="reply" name="replytext" maxlength="500" cols="80" rows="6"></textarea>
</div>
<input type="text" id="lookup" name="reply-lookup" />
<input type="submit" id="submit" name="post" value="Post" />
</form>
</div>
JQ
// -------------------------reset the forms -------------------------
function reset(formhtml) {
$('#posttopic, #comment, #reply').text(""); //will not clear #reply
$('#lookup, #reply').val(""); //this will clear #reply
} }
.val()
is the main method to get inputted data. .val()
是获取输入数据的主要方法。 Heres a quote from http://api.jquery.com/val/ .这是来自http://api.jquery.com/val/的引用。
The .val() method is primarily used to get the values of form elements such as input, select and textarea. .val() 方法主要用于获取表单元素的值,例如 input、select 和 textarea。 In the case of elements, the .val() method returns an array containing each selected option;在元素的情况下, .val() 方法返回一个包含每个选定选项的数组; if no option is selected, it returns null.如果未选择任何选项,则返回 null。
.text()
does not work as textarea
is still an input. .text()
不起作用,因为textarea
仍然是输入。
From JQuery documentation:从 JQuery 文档:
The .text() method cannot be used on form inputs or scripts. .text()方法不能用于表单输入或脚本。 To set or get the text value of input or textarea elements, use the .val() method.要设置或获取 input 或 textarea 元素的文本值,请使用.val()方法。 To get the value of a script element, use the .html() method.要获取脚本元素的值,请使用.html()方法。
怎么样:
$('#form')[0].reset(); // use the id of the form
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.