jQuery .val()返回空字符串 [英] jQuery .val() returns empty string
本文介绍了jQuery .val()返回空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尽管这可能是一个简单的问题,但感觉就像jQuery困扰着我.我的代码获取到我想要获取DOM元素的DOM元素,但是当我添加.val()时,它将返回一个空字符串.
It feels like jQuery is messing with me, although it's probably a simple issue. My code get's to the DOM-element that i want to get the value of, but when i add .val() it returns an empty string.
$('tr.invoice td[data-attr=Payed] div.true').closest('tr').find('td[data-attr=Expiration]')
Returns this:
[<td data-attr="Expiration">2012-04-22</td>,<td data-attr="Expiration">2012-05-21</td>,<td data-attr="Expiration">2012-06-18</td>]
您可以看到DOM元素具有值,但是如果我在JS字符串中添加.val(),则它仅返回空字符串(不是未定义的,空的)
As you can see the DOM-elements has values, but if i add .val() to the JS-string it returns empty strings only (not undefined, empty)
推荐答案
方法val()
用于input
和select
元素.
您应使用 html()
或
You should use html()
or text()
methods instead.
$('tr.invoice td[data-attr=Payed] div.true')
.closest('tr')
.find('td[data-attr=Expiration]')
.each(function() {
// getting inner HTML
var html = $(this).html();
}
);
这篇关于jQuery .val()返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文