简体   繁体   English

如何获取动态生成的td属性和内部元素id标记的值,且在js / jquery中具有tr的id

[英]how to get values of dynamically generated td attributes and inside elements id tags with tr having id in js/jquery

I trying to build a calendar of events with PHP, jQuery and ajax. 我试图用PHP,jQuery和Ajax构建事件日历。 The calendar is integrated in HTML table, the rows and fields of a table are generated dynamically according to the number of days in a certain month. 日历集成在HTML表格中,表格的行和字段是根据特定月份中的天数动态生成的。 I need to get the values of td elements by id (days in calendar) and the values of divs by id that are created inside the table (event id number). 我需要通过id(在表中创建的天数)获取td元素的值,并通过在表内部创建的id(事件id号)获取div的值。 I need unique id numbers in order create/edit/delete events and store them in database. 我需要唯一的ID号才能创建/编辑/删除事件并将其存储在数据库中。

The problem is that id number is generated with rows and fields dynamically and I dont know them in advance. 问题是ID号是动态生成的,具有行和字段,而我事先并不知道。 Otherwise if I knew the id number in advance it would not be a problem. 否则,如果我提前知道ID号,就不会有问题。 Please give me any hint in this situation. 在这种情况下,请给我任何提示。

These loops are generating table rows and fields id numbers: 这些循环生成表行和字段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>';

![enter image description here][1] ![在此处输入图片描述] [1]

And this is a how a genrated table looks. 这就是生成表的外观。

日历](http://screencast.com/t/nuVDhdoF![在此处输入图片说明 )

This is a script: 这是一个脚本:

http://screencast.com/t/yhWbeEC1nfxo http://screencast.com/t/yhWbeEC1nfxo

You start off with the list of rows with class calendarRow , then you drill into each row, and get the list of days in that row, and then the list of events for each day: 您可以从使用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());
            });
        });
    });
});

Note you don't have to iterate through all the rows or all the days to get event details. 注意:您不必通过所有行或所有的日子迭代获得事件的细节。 You can just do this: 您可以这样做:

$(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM