gpt4 book ai didi

javascript - 获取 div 内表格中单元格的 ID(JS 和 HTML)

转载 作者:行者123 更新时间:2023-11-30 18:57:09 25 4
gpt4 key购买 nike

您好,我有一个 PHP 脚本,可以根据特定条件绘制表格。绘制表格时,它位于名为“window”的 div 内,表格名为“visi”,然后我想使用下面的代码获取表格中每个单元格的 ID。我可以毫无问题地获得类名,但绝对不能从 id 中得到任何东西。谁能告诉我我做错了什么?我在一个不在 div 内的表上尝试了一段类似的代码,它工作正常。任何帮助都会很棒,我希望代码有意义。

function confirm_allocate() {
var msg = ""
var tab = document.getElementById('visi');
var r = tab.rows.length

for (i=0; i<r; i++){
cc = tab.rows.cells.length
for (col=0; col<cc; col++){
x=document.getElementById('visi').rows.cells;

iden = x[col].className
ref = x[col].id

msg += "Class =" + iden + " /// Id =" + ref + "\r"
}
}
alert (msg )
}

如果有帮助,这是绘制表格的代码(但这是在获取表格信息后使用 js/ajax 调用的)

<?php

$table = "" ;
include '../database_sql/dbconnect.php' ;
include '../functions/job_details.php';
include '../functions/check_date.php';
include '../functions/stock_list.php' ;
include '../functions/allocated_on_date.php' ;

$jobdate = $_GET['jobdate'] ;
$jobnumber = $_GET['jobnumber'] ;
$jobname = $_GET['jobname'] ;

$screens = screens_per_job($jobnumber,$size) ;

$table = "<h2 align= 'center' > $jobname (Job Number $jobnumber) : Screens required : $screens </h2>" ;
$table .= "<table id='visi' width='480px' align='center' cellspacing='1' cellpadding='1' border='1' ><tr>" ;

// get stock list from DB
stock_list() ;
$len = count( $stock);

$evresult = mysql_query("SELECT * FROM event WHERE jobnumber = '$jobnumber' ") ;
$event_row = mysql_fetch_array($evresult);

for ($counter = 0; $counter < $len; $counter++) {
$item = $stock[$counter] ;
$items = $item . "s" ;
$booked_for_job = $event_row[$items] ;
$result = mysql_query("SELECT * FROM $item ") ;
allocated_on_date($jobdate) ; // function

if ($booked_for_job) {
$count = 1 ;
$table .= "<td >$item<br> [$booked_for_job to Allocate] </td> " ;

WHILE ($row = mysql_fetch_array($result)) { ;
$booked_job = $screens[$item][$count]["job"] ; // from the allocated_on_date($jobdate) function
$description = $row['trailer_id'];
$class = $items ;
$id_items = $items . $count ;
$count ++ ;

if ($booked_job == $jobnumber) { // allocated to current job
$table .= "<td class='truckbox' > <div class='$class' id='$id_items' onClick='allocate(\"$booked_for_job\",\"$id_items\")' > " ;
$table .= "$num </div> </td>" ;

} ELSEIF ($booked_job === 0 ) { // available to allocated
$class .= "g" ;
$table .= "<td class='truckbox' > <div class='$class' id='$id_items' onClick='allocate(\"$booked_for_job\",\"$id_items\")' > " ;
$table .= "$num </div> </td>" ;

} ELSE { // allocated to ANOTHER job
$class .= "a" ;
$table .= "<td class='truckbox' > <div class='$class' id='$items' > " ;
$table .= "</td> " ;
}

} // while

$table .= "</tr>" ;
} ; // if
}; //for


$table .= "</table> " ;

$table .= "<table width='200px' align='center' cellspacing='12' cellpadding='1' ><tr>" ; // draw table buttons close and allocate
$table .= "</tr><tr>" ;
$table .= "<td class='truckbox' <div class='yesbutton' id='yes' onClick='confirm_allocate()' ; return false ; > ";
$table .= "<td class='truckbox' <div class='nobutton' id='no' onClick='hide()' > ";
$table .= "</tr></table> ";

echo $table ; // finally draw table

include '../database_sql/dbclose.php' ;

?>

最佳答案

我没有看到任何 id正在生成的表格单元格(td 元素)的属性。所以ref将只是空白或未定义。

如果这不是问题,那么可能是 .rows.cells .尝试 .getElementsByTagName('td')反而。我认为该行应该在循环之外,因为它不会在每次迭代中发生变化。

此外,您的 HTML 的后半部分是错误的 - 您没有关闭 td标签。而不是 <td class='truckbox' <div class...应该是<td class='truckbox'><div class...

关于javascript - 获取 div 内表格中单元格的 ID(JS 和 HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1618107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com