简体   繁体   中英

Javascript/Jquery UNDEFINED textarea value

I run a website and there is an area to send messages with AJAX. I've noticed that some messages say undefined and therefore people can't use the message system. The following is the code used to get the value from the text area, whats wrong with it? I'm thinking that some browsers don't accept it so how can I fix it? Thanks

<textarea id="message"></textarea>
$('textarea#message').val()

The val() method get the value attribut, but tag textarea has no value attribut. The text inside Textarea is saved inside the tags <textarea> and </textarea> , so you have to use $('textarea#message').text() . It should work.

  <textarea >some text</textarea >

   not like this

   <textarea value="some text"></textarea >

  <textarea id="message"></textarea>
  $('textarea#message').text();

demo

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM