加载 DOM 后无法读取动态创建的复选框的属性
Cannot Read Property of Dynamically Created Checkbox after DOM is Loaded
我有一个应用程序,允许用户选择一门或多门课程。用户可以选择保存所选课程,稍后再回来完成该过程。当用户返回时,我会重新创建复选框列表。然后,我尝试在该div 中找到所有输入复选框。我将其记录到控制台,它返回一个空集合。如何获取复选框属性?
填充有复选框的空div。
<div class="courseList applyBackgroundColor" id="UserCheckList">
</div>
我在哪里做帖子并使用结果创建动态文本框。
var createCourse = function(studentID)
{
var StudentCourseList = '<table><tbody>';
do post here
$.each(result, function (index, item) {
StudentCourseList += '<td valign="top" style="width:1%" id=td.ck.' +
item.ID + '><div class=""><input type="checkbox" class="checkbox"
id="'+ item.ID + '" value="' + item.ID + '" /></div></td>
<td valign="top" style="width:30%;padding:.25em;" id="' + item.ID +
'"><span id="span.' + item.ID + '" title="' + item.Description + '">'
+ item.Description +'</span></td>';
}
$('#UserCheckList').html(StudentCourseList );
}
在页面加载时检查是否有学生 ID。
$(function(){
var studentID = $('#studentID').val();
if(studentID !==''){
createCourse(studentID);
var listCheckUsers = $('.courseList').find('input[type=checkbox]');
console.log(listCheckUsers);
如果我在listCheckusers旁边放置一个断点并对其进行调试,则控制台中显示的结果如下所示:
Object[input#MAC201.checkbox attribute value = "MATH 201",
input#ENC101.checkbox attribute value = "ENGLISH 101",....]
}
没有断点,我看到一个空对象
Object[]
});
已更新:添加确切的 JQuery 消息。
//This is shown when I do not use a breakpoint.
1. jQuery.fn.init[0]
1. context: document
2. length: 0
3. prevObject: jQuery.fn.init[1]
1. 0: div#UserCheckList.Display
2. context: document
3. length: 1
4. selector: "#UserCheckList"
5. __proto__: jQuery[0]
4. selector: "#UserCheckList input[type=checkbox]"
5. __proto__: jQuery[0]
你在表元素中缺少类名courseList
。
更新我使用 setTimeout 模拟了一个 Ajax 请求。 您可以删除 setTimeout 代码并放置一个 Ajax 请求。 返回数据后,使用数据运行回调函数。
function getResults(studentID, callback) {
// Async call.
setTimeout(function() {
// Replace generator with Ajax call
var result = [];
for (var i = 0; i < 10; i++) {
var item = {
ID: i,
"Description": "Result #" + i
};
result.push(item);
}
// Run this when data returns.
callback(result);
}, 3000);
// Show loading while we wait.
$('.UserCheckList').html('loading...');
}
function showResults(result) {
var StudentCourseList = '<table class="courseList"><tbody>';
$.each(result, function(index, item) {
StudentCourseList += '<tr><td valign="top" style="width:1%" id=td.ck.' +
item.ID + '><div class=""><input type="checkbox" class="checkbox"'
id="' + item.ID + '" value="' + item.ID + '" /></div></td>'
<td valign="top" style="width:30%;padding:.25em;" id="' + item.ID +
'"><span id="span.' + item.ID + '" title="' + item.Description + '">' + item.Description + '</span></td></tr>';
});
$('.UserCheckList').html(StudentCourseList);
}
$(function() {
var studentID = $('#studentID').val();
if (studentID !== '') {
getResults(studentID, function(results) {
// callback when results are complete.
showResults(results);
var listCheckUsers = $('.courseList').find('input[type=checkbox]');
console.log(listCheckUsers);
});
}
}); //end
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserCheckList"></div>
相关文章:
- 在JavaScript中动态创建复选框
- 基于表中的列动态创建复选框
- 为xml谷歌地图生成的每个标记创建复选框
- 将“已创建”复选框设置为选中不起作用
- extjs 3.4使用combo store创建复选框组项目
- 从AJAX响应动态创建复选框
- 使用Font Awesome与AngularJs创建复选框
- 使用Bootstrap Glyphicons创建复选框功能
- Javascript 已创建复选框未正确关闭标记
- 从数组列表java中动态创建复选框
- 动态创建复选框单击事件,选择整行,而不仅仅是jquery数据表中的复选框.为什么呢?
- 应用iCheck (jQuery插件)来动态创建复选框
- 动态创建复选框;在ExtJS中点击按钮不会被选中
- 用javascript创建复选框
- 在XUL中使用DOM JavaScript动态创建复选框
- 创建复选框
- 可以在组合框中创建复选框
- 如何使用Jquery动态创建复选框
- 如何通过函数创建复选框
- 如何创建复选框的角度模式表单检查表