Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中选择动态生成的元素_Javascript_Jquery_Html - Fatal编程技术网

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

Javascript 在jQuery中选择动态生成的元素,javascript,jquery,html,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">

我有以下代码:

<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>
但是它返回空字符串

试试这个

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

尝试使用
。家长

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

因为父项是div,而不是tr。请阅读文档以了解更多信息@父母('tr')
似乎做OP想要做的事。可能是打字错误。
parents()
是一个错误的选择。@epascarello这样做有什么原因吗?我希望它的可读性,但这也包括
标记。你为什么使用
.find(“strong”).html()
而不是
.text()
?我更喜欢在这种情况下使用html()来获取所有内容@newbieparents()如果是嵌套表,则会提供多个元素。这也是因为。最近的('td')会看着TR的父母!
$('.question-choices').on('change', function() {
    console.log( $(this).parents('tr').closest('td').text() );
});