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

标签 javascript jquery html

我有这个代码:

<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>

我想从第一个 <td> 中获取文本来自 radio button :

到目前为止我尝试的是:

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

但这会返回空字符串

最佳答案

试试这个;

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

关于javascript - 在 jQuery 中选择动态生成的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20195772/

相关文章:

javascript - 如何从 Outlook 加载项创建新电子邮件?

javascript - JQuery error() 函数在 IE 中不起作用

php - 使用 jquery ajax 处理服务器端验证的最佳方法

javascript - 在 Google Chrome 扩展程序中使用 javascript 访问 Google 的 URL shortener API

jquery - 使复选框引起 jQuery 验证

html - 使用 jQuery 将 TD 隐藏到 DIV 中

javascript - 对象 HTMLInputElement attr 不是函数

javascript - 如何定位在 php 中没有任何 ID 或标记的 <span> 元素的 innerHTML?

javascript - jQuery on click事件多次触发

jquery - 如果 Rails 中的 text_field_tag 表单助手被禁用,它不会提交吗?