how to get the value of a textarea in jquery?

前端 未结 12 2067
甜味超标
甜味超标 2020-11-27 17:55

i have this form and im trying to get the value from the text area. for some reason it doesn\'t want to.

相关标签:
12条回答
  • 2020-11-27 18:09

    all Values is always taken with .val().

    see the code bellow:

    var message = $('#message').val();
    
    0 讨论(0)
  • 2020-11-27 18:13

    You should check the textarea is null before you use val() otherwise, you will get undefined error.

    if ($('textarea#message') != undefined) {
       var message = $('textarea#message').val();
    }
    

    Then, you could do whatever with message.

    0 讨论(0)
  • 2020-11-27 18:13

    You can also get value by name instead of id like this:

    var message = $('textarea:input[name=message]').val();
    
    0 讨论(0)
  • 2020-11-27 18:18

    you should use val() instead of html()

    var message = $('#message').val();
    
    0 讨论(0)
  • 2020-11-27 18:20

    You don't need to use .html(). You should go with .val().

    From the doc of .val():

    The .val() method is primarily used to get the values of form elements such as input, select and textarea. When called on an empty collection, it returns undefined.

    var message = $('#message').val();
    
    0 讨论(0)
  • 2020-11-27 18:21

    in javascript :

    document.getElementById("message").value
    
    0 讨论(0)
提交回复
热议问题