[英]how to get values of dynamically generated td attributes and inside elements id tags with tr having id in js/jquery
我试图用PHP,jQuery和Ajax构建事件日历。 日历集成在HTML表格中,表格的行和字段是根据特定月份中的天数动态生成的。 我需要通过id(在表中创建的天数)获取td元素的值,并通过在表内部创建的id(事件id号)获取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]
这就是生成表的外观。
)
这是一个脚本:
您可以从使用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"));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.