私は一日中これに取り組んでいます。ダイアログテキストを動的に変更する方法を理解しましたが、ユーザーが「はい」または「いいえ」から選択するのを待って、ダイアログボックスに各生徒の名前を表示する方法が本当にわかりませんか? アラートは for in ループが機能することを示していますが、ダイアログの結果は私が望むものではありません。
各選択を追跡し、それに応じて Javascript オブジェクトを変更するロール プログラムを呼び出したいと思います。
ダイアログのように、Dennis [ yes or no ?] 、 Zoe [ yes or no?] などが表示されます... ユーザーが「いいえ」をクリックすると、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/