我已经为此一整天了。我已经想出了如何动态更改对话框文本,但是,我真的不知道如何让每个学生姓名显示在对话框上,等待用户从“是”或“否”中进行选择?现在警报显示 for in 循环有效,但对话框上的结果不是我想要的。
我想调用滚动程序来跟踪每个选择并相应地修改 Javascript 对象。
就像在对话框中出现 Dennis [是或否?],Zoe [是或否?] 等等......当用户单击“否”时,JS 对象将被修改以反映更改。
感谢任何好心的帮助者。
$(document).ready(function(){
var students = {
"dennis":true,
"zoe":true,
"ken":true,
"carol":true
};
// a function to count elements in an JS object
function countObject(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// get the number of elements in an object
var studentTotal = countObject(students);
// display the initial text on the page
$('#totalStudent').text("Our class has " + studentTotal + " students in total.");
// click event
$('#callTheRoll').click(function(){
// use a for loop to call everyone in the object
for(var element in students){
alert("Is " + element + " in class?");
$('#dialogText').text("Is " + element + " in class?");
// pop up the dialog
$( "#dialog-confirm" ).dialog({
resizable: false,
height:210,
modal: true,
buttons: {
"Yes": function() {
$( this ).dialog( "close" );
},
"No": function() {
$( this ).dialog( "close" );
}
}
}); // end of the custom-made confirm button setting
}
});
});
这是我的 jsfiddle:http: //jsfiddle.net/dennisboys/TtJdC/1/