如何使用jquery清空文本区域中的消息?

前端之家收集整理的这篇文章主要介绍了如何使用jquery清空文本区域中的消息?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
– 更新 –

我想在回调中的textarea中清空消息。

任何人都可以告诉我如何清空它吗?

我试过$(“#message”)。empty(),但它不会清空它。

提前致谢。

  1. <form method="post" id="form" action="index.PHP/admin/messages/insertShoutBox">
  2.  
  3. <input name="user" id="nick" value="admin" type="hidden">
  4. <p class="messagelabel"><label class="messagelabel">Message</label>
  5. <textarea id="message" name="message" rows="2" cols="40"></textarea>
  6. <input disabled="disabled" id="send" value="Sending..." type="submit"></p>
  7.  
  8. </form>

完整代码

  1. $("#form").submit(function(){
  2. if(checkForm()){
  3. var nick = inputUser.attr("value");
  4. var message = inputMessage.attr("value");
  5. //we deactivate submit button while sending
  6. $("#send").attr({ disabled:true,value:"Sending..." });
  7. $("#send").blur();
  8. //send the post to shoutBox.PHP
  9. $.ajax({
  10. type: "POST",url: "index.PHP/admin/dashboard/insertShoutBox",data: $('#form').serialize(),complete: function(data){
  11. messageList.html(data.responseText);
  12. updateShoutBox();
  13. $('#message').val('').empty();
  14. //reactivate the send button
  15. $("#send").attr({ disabled:false,value:"SUBMIT !" });
  16. }
  17. });
  18. }
  19. else alert("Please fill all fields!");
  20. //we prevent the refresh of the page after submitting the form
  21. return false;
  22. });

解决方法

  1. $('#message').val('');

说明(from @BalusC):

textarea是具有值的输入元素。你实际上想要“清空”该值。因此对于每个其他输入元素(input,select,textarea),您需要使用element.val(”);.

另见docs

猜你在找的jQuery相关文章