简体   繁体   English

在jQuery中选择动态生成的元素

[英]Selecting dynamically generated element in jQuery

I have this code: 我有这个代码:

<c:forEach items="${ sample }" var="test">
    <tr>
        <td><strong>${ test.test }.</strong></td> // I want to get the text
        <td>
            <fieldset class="span11">
                <div class="control-group">
                    <label class="control-label"><strong>${ test.blah }</strong></label>
                    <div class="controls" id="question${ test.blah }">
                        <c:forEach var="beng" items="${ anotherSample }">
                            <form:radiobutton path="boom" class="question-choices" data-label="${ choices }" value="${ beng-boom }"/><br>
                        </c:forEach>
                    </div>
                </div>
            </fieldset>                 
        </td>
    </tr>
</c:forEach>

I want to get the text from the first <td> from the radio button : 我想从radio button的第一个<td>获取文本:

What I tried so far is: 到目前为止我尝试的是:

$('.question-choices').on('change', function() {
    console.log( $(this).parent('tr').closest('td').text() );
});

But this returns empty string 但是这会返回空字符串

try this; 尝试这个;

$('.question-choices').on('change', function() {
    console.log( $(this).closest('tr').find('td:first').find("strong").html() );
});

parent() only gets a direct parent. parent()只获得直接父级。

Try using .parents : 尝试使用.parents

$('.question-choices').on('change', function() {
    console.log( $(this).parents('tr').closest('td').text() );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM