更改活动

时间:2016-05-24 21:26:11

标签: javascript jquery html ajax event-binding

我有一个通过AJAX分页的复选框表。我正在尝试为单击其中一个新复选框创建一个on click事件。单击它时,我将需要根据该标准的ID更新一系列ID。

问题是,在AJAX调用之后填充输入时,我无法弄清楚如何让我的Javascript响应。如果我在视图中加载输入,则点击事件可以正常工作,但如果它通过AJAX返回,则无法识别。

我已经查看了几个类似的问题,包括:

我找不到上述任何问题的解决方案。我无法确定这里缺少的东西。

HTML

<table summary="Standard Listing Table">
    <thead>
        <tr>
            <th scope="col"></th>
            <th scope="col">Code</th>
            <th scope="col">Description</th>
        </tr>
    </thead>
    <tbody id="pl-standard-listing">
        <tr>
             <td>
                  <input checked="" name="standards[]" type="checkbox" value="8343">
             </td>
             <td>K.CC.A.1</td>
             <td>Count to 100 by ones and by tens.</td>
        </tr>
    </tbody>
</table>

点击事件的Jquery

$('#pl-standard-listing tr td').on('click', 'input[type=checkbox]', function() {
    console.log("Clicked.");
    console.log(this);
});

为API中的所有标准添加输入

// Select table and whipe current standards that are listed
var table = document.getElementById('pl-standard-listing');
$('#pl-standard-listing tr').remove();
for(var iCount = 0; iCount < currentStandards.length; iCount++) {
    // Check if the standard is in the list of included standards with this grade group
    if(standardIds.indexOf(currentStandards[iCount].id) === -1) {
        $('#pl-standard-listing').append($('<tr><td><input name="standards[]" type="checkbox" value="' + currentStandards[iCount].id + '" /></td><td>' + currentStandards[iCount].code + '</td><td>' + currentStandards[iCount].description + '</td></tr>'));
    } else {
        $('#pl-standard-listing').append($('<tr><td><input checked name="standards[]" type="checkbox" value="' + currentStandards[iCount].id + '" /></td><td>' + currentStandards[iCount].code + '</td><td>' + currentStandards[iCount].description + '</td></tr>'));
    }
}

标准在视图中正确加载,我只是无法通过单击复选框获取事件处理程序。非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:3)

从点击事件中删除tr td应该有效。

$('#pl-standard-listing').on('click', 'input[type=checkbox]', function() {
console.log("Clicked.");
console.log(this);
});
相关问题