0

我基本上是在尝试获取 textarea 中的内容以进行比较,但是即使我更改 textarea 框中的内容,.text() 或 .val() 也不会改变

$(document).ready(function() {
      $('#getContents').click(function() {
           console.log($('#txtarea').text());
      });
 });


 <textarea id="txtarea">test</textarea>
 <input type="submit" id="getContents" />

http://jsfiddle.net/THTsG/

4

4 回答 4

6

使用 atextarea时,请使用.val()(我看到您说您尝试过,但确实有效):

演示:http: //jsfiddle.net/tymeJV/THTsG/1/

问:

$('#getContents').click(function() {
    console.log($('#txtarea').val());
});
于 2013-09-20T14:21:05.073 回答
1

如果使用

.val();

代替

.text();

一切似乎都正常。

JSFiddle

于 2013-09-20T15:24:36.627 回答
0

First off, thanks for using jsFiddle.

Use .val, just as tymeJV says. However, note that you don't have to wait until the submit is clicked. You can have code sense when the textarea changes. However, it does wait until the user is done changing it and clicks something other than the text area.

Here's what I drafted up for you at jsFidde http://jsfiddle.net/khV4T/.

$(document).ready(function() {
    $('#txtarea').on('change', function() {
    console.log($('#txtarea').val());
    });
});
于 2013-09-20T14:46:07.783 回答
0

我已经更新了你的 jsFiddle。建议使用 .on 对您的对象进行动态控制。

$(document).ready(function() {
    $('#getContents').on("click", function() {
        alert($('#txtarea').val());
    });
});
于 2013-09-20T14:25:01.837 回答