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("")
). 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. Heres a quote from http://api.jquery.com/val/ .
The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.
.text()
does not work as textarea
is still an input.
怎么样:
$('#form')[0].reset(); // use the id of the form
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.