[英]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.