$ .html没有textarea值

时间:2016-04-15 10:48:56

标签: javascript jquery html

当我们尝试使用下面的代码获取div的html时,除了我在textarea或输入字段中输入的文本之外,它具有所有数据

var mywindow =  $(printDiv).html();

mywindow将包含除textarea文本,单选按钮值或inputfield之外的所有html。

我调查过this question但是使用克隆会提供任何文字。

2 个答案:

答案 0 :(得分:2)

The thing to remember is that .html() does exactly what it says: it gives you the HTML currently in the DOM within that node. But the thing is, content entered by the user, and more generally the content of HTML input fields, is not a part of the HTML. Entering text into a textarea or checking a checkbox or radio button doesn't change the HTML one bit. It does update the internal memory representations of the individual DOM nodes, but this isn't represented in the HTML.

Whenever you want to get content from an input element on the page, you have to use .val() instead of .html(). The .val() function also does what it says: it gets you the value of the input field.

答案 1 :(得分:1)

要在textarea中获取文本,您必须在JQuery中使用.val()。例如

var text = $('#textarea_id').val();
console.log(text);

第二行将其记录到控制台,以便您检查它是否有效。