我尝试使用PHP,jQuery和ajax构建事件日历。 日历集成在HTML表格中,表格的行和字段根据特定月份的天数动态生成。我需要通过id(日历中的天数)获取td元素的值以及在表内创建的div的值(事件ID号)。我需要唯一的ID号来创建/编辑/删除事件并将它们存储在数据库中。
问题是id号是动态生成的行和字段,我不提前知道它们。否则,如果我事先知道身份证号码就不会有问题。 在这种情况下,请给我任何暗示。
这些循环正在生成表行和字段id号:
$month_row = 1;
$calendar .= "<table id='calendar-grid' class='table table-bordered calendar-grid'>";
$calendar .= "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
$calendar .= "<tr id='row_".$month_row."' class='calendar_row'>";
for($i = 1; $i <= $offset; $i++){
$calendar .= "<td></td>"; // generate empty fields according to offset number
}
for($day = 1; $day <= $daysInMonth; $day++) {
if( ($day + $offset - 1) % 7 == 0 && $day != 1) {
$month_row++;
$calendar .= "</tr><tr id='row_".$month_row."' class='calendar_row'>";
$rows++;
}
if($currentDay == $day && date("F") == date("F", $date)){
$calendar .= "<td id='date_". date("Y-m").'-'.$day ."' class='eventList currentDay'>" . $day . "</td>";
}
else{
if(($day + $offset - 1) % 7 == 0){
$calendar .= "<td id='date_". date("Y-m").'-'.$day ."' class='eventList Sunday'>" . $day . "</td>";
}else{
$calendar .= "<td id='date_". date("Y-m").'-'.$day."' class='eventList'>" . $day . "</td>";
}
}
}
while( ($day + $offset) <= $rows * 7)
{
$calendar .= "<td></td>";
$day++;
}
$calendar .= "</tr>";
$calendar .= "</table>";
echo '</div>';
![在此处输入图片说明] [1]
这就是生成表格的样子。
)
这是一个脚本:
答案 0 :(得分:1)
首先使用类calendarRow
的行列表,然后深入查看每一行,获取该行中的日期列表,然后获取每天的事件列表:
$(function() {
// Get all the rows
var calendarRows = $("tr.calendar_row");
// Drill down to each row
calendarRows.each( function(rowIndex, selectedRow) {
// Print out the row ID
console.log("Row ID = "+selectedRow.attr("id"));
var calendarDays = selectedRow.find("td.eventList");
// Drill down to each day
calendarDays.each( function(dayIndex, selectedDay) {
// Print out the day ID
console.log("Day ID = "+selectedDay.attr("id"));
var events = selectedDay.find("div.eventStyle");
// Drill down to each event
events.each( function(eventIndex, selectedEvent) {
// Print out the event details
console.log("Event number "+eventIndex+" has ID="+selectedEvent.attr("id")+" and text="+selectedEvent.text());
});
});
});
});
请注意,您 无法遍历所有行或所有日期以获取事件详细信息。你可以这样做:
$(function() {
var events = $("div.eventStyle");
// Drill down to each event
events.each( function(eventIndex, selectedEvent) {
// Print out the event details
console.log("Event number "+eventIndex+" has ID="+selectedEvent.attr("id")+" and text="+selectedEvent.text()+" and has parent with ID="+selectedEvent.parent().attr("id"));
});
});