我有下面的表单,当我关闭表单时,我想清除任何用户输入。我是用以下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}
发布于 2013-12-05 22:05:43
.val()是获取输入数据的主要方法。这是http://api.jquery.com/val/的引文。
.val()方法主要用于获取表单元素的值,例如输入、选择和文本区域。对于元素,.val()方法返回包含每个选定选项的数组;如果没有选择选项,则返回null。
.text()不能工作,因为textarea仍然是输入。
发布于 2013-12-05 22:08:31
来自JQuery文档:
http://api.jquery.com/text/方法不能用于表单输入或脚本。若要设置或获取输入或文本区域元素的文本值,请使用http://api.jquery.com/val/方法。要获得脚本元素的值,请使用.html()方法。
发布于 2013-12-05 22:16:09
不如:
$('#form')[0].reset(); // use the id of the formhttps://stackoverflow.com/questions/20411764
复制相似问题