2013-05-12 65 views
0
<table > 
<tr align="left" id="progNav" class="width_150 margin_top20"> 


        <core:forEach items="${fields}" var="field" > 
         <th value="${field.id} .." <core:if test="${field.id == fieldId }">id="bg_active_1" class="selected" </core:if>><a href="" class="bg_link1">${field.programmeField}</a></th> 
        </core:forEach>   

</tr> 
</table> 

如何获得class =“selected”或id =“bg_active_1”的值? 列表 我曾经尝试这样做: -如何在jquery中动态获取值?

var fieldId=$('#progNav').parent().find('#bg_active_1').val(); 
var fieldId=$('#progNav').parent().find('th.selected').val(); 
var fieldId=$("#bg_active_1").val(); 

,还有更多,但不知道为什么我每次都获得空值!我弄糊涂头请帮助我。

+0

@SLaks ya它独特的:O – Jordon 2013-05-12 15:29:20

回答

0

我得到什么我从这个想..

VAR所有元素fieldId = $(” #bg_active_1" )ATTR( '值')。

2

正如文档中清楚说明的那样,.val()获得了表单元素的值。
您没有任何表单元素。

我怀疑你实际上是想得到元素的内容;您可能需要.text().httml()函数。

3

$('.selected')得到所有元素与class="selected"

$('.selected').each(function(_, value) { 
    // prints their HTML code 
    console.log('.selected: '+$(value).html()); 
    // prints their (and children) text content 
    console.log('.selected: '+$(value).text()); 
}); 

$('[id="bg_active_1"]')得到与id="bg_active_1"

$('[id="bg_active_1"]').each(function(_, value) { 
    // prints their HTML code 
    console.log('[id="bg_active_1"]: '+$(value).html()); 
    // prints their (and children) text content 
    console.log('[id="bg_active_1"]: '+$(value).text()); 
});