– 更新 –
我想在回调中的textarea中清空消息。
任何人都可以告诉我如何清空它吗?
我试过$(“#message”)。empty(),但它不会清空它。
提前致谢。
- <form method="post" id="form" action="index.PHP/admin/messages/insertShoutBox">
- <input name="user" id="nick" value="admin" type="hidden">
- <p class="messagelabel"><label class="messagelabel">Message</label>
- <textarea id="message" name="message" rows="2" cols="40"></textarea>
- <input disabled="disabled" id="send" value="Sending..." type="submit"></p>
- </form>
完整代码
- $("#form").submit(function(){
- if(checkForm()){
- var nick = inputUser.attr("value");
- var message = inputMessage.attr("value");
- //we deactivate submit button while sending
- $("#send").attr({ disabled:true,value:"Sending..." });
- $("#send").blur();
- //send the post to shoutBox.PHP
- $.ajax({
- type: "POST",url: "index.PHP/admin/dashboard/insertShoutBox",data: $('#form').serialize(),complete: function(data){
- messageList.html(data.responseText);
- updateShoutBox();
- $('#message').val('').empty();
- //reactivate the send button
- $("#send").attr({ disabled:false,value:"SUBMIT !" });
- }
- });
- }
- else alert("Please fill all fields!");
- //we prevent the refresh of the page after submitting the form
- return false;
- });
解决方法
- $('#message').val('');
说明(from @BalusC):
textarea是具有值的输入元素。你实际上想要“清空”该值。因此对于每个其他输入元素(input,select,textarea),您需要使用element.val(”);.
另见docs