[英]How to clear text from textarea input?
我有以下表单,我想在关闭表单时清除所有用户输入。 我使用以下 JQ 函数执行此操作,但是 textarea 元素不会以相同的方式清除( #reply
只会使用.val("")
清除)。 我不明白为什么会这样。
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()
是获取输入数据的主要方法。 这是来自http://api.jquery.com/val/的引用。
.val() 方法主要用于获取表单元素的值,例如 input、select 和 textarea。 在元素的情况下, .val() 方法返回一个包含每个选定选项的数组; 如果未选择任何选项,则返回 null。
.text()
不起作用,因为textarea
仍然是输入。
怎么样:
$('#form')[0].reset(); // use the id of the form
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.