我有一个处理表中动态 ID 的 javascript 函数。
桌子:
<table border = "0" width = "95%" class = "table table-striped table-bordered" id = "tblInfoChecks">
<thead>
<tr>
<th><center>#</center></th>
<th><center>Category</center></th>
<th><center>Name</center></th>
<th><center>Date</center></th>
<th><center>Due Date</center></th>
<th><center>Allocation</center></th>
<th><center>Status</center></th>
<th><center>TAT</center></th>
<th><center>Action</center></th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM checks WHERE candidate_id = '$case_id' AND client_id = '$client_id'";
$res = mysql_query($q);
$numR = mysql_numrows($res);
$y = 0;
while($y < $numR){
$m = $y + 1;
$check_id = mysql_result($res, $y, "case_id");
$check_name = mysql_result($res, $y, "check_name"); //hidden
$check_cat = mysql_result($res, $y, "check_category"); //hidden
$elements = mysql_result($res, $y, "elements"); //hidden
//verified information -- hidden
$ver_status = mysql_result($res, $y, "ver_status");
$ver_remarks = mysql_result($res, $y, "ver_remarks");
$ver_action = mysql_result($res, $y, "ver_action");
$overall_status = mysql_result($res, $y, "overall_status");
$check_date = mysql_result($res, $y, "date_to_process");
$check_due_date = mysql_result($res, $y, "due_date");
$ver_id = mysql_result($res, $y, "verifier_id");
$check_status = mysql_result($res, $y, "status");
//hidden elements
echo '<input type = "text" style = "" id = "txtInfoCheckID'.$y.'" value = "'.$check_id.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckName'.$y.'" value = "'.$check_name.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckCat'.$y.'" value = "'.$check_cat.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckElements'.$y.'" value = "'.$elements.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckVerStatus'.$y.'" value = "'.$ver_status.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckVerRemarks'.$y.'" value = "'.$ver_remarks.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckVerAction'.$y.'" value = "'.$ver_action.'" />';
echo '<input type = "text" style = "" id = "txtInfoCheckOverallStatus'.$y.'" value = "'.$overall_status.'" />';
//get verifier name
$ver = "SELECT name FROM employees WHERE id = '$ver_id'";
$ver_res = mysql_query($ver);
$ver_numR = mysql_numrows($ver_res);
if($ver_numR != 0){
$ver_name = mysql_result($ver_res, 0 ,0);
}
//compute TAT
$check_date_sec = strtotime($check_date);
$today = strtotime(date("m/d/Y", time()));
$tat = $today - $check_date_sec;
$time_arr = secondsToTime($tat);
$final_tat = $time_arr["d"]; //tat
echo '
<tr>
<td><center>'.$m.'</center></td>
<td><center>'.$check_cat.'</center></td>
<td><center>'.$check_name.'</center></td>
<td><center>'.$check_date.'</center></td>
<td><center>'.$check_due_date.'</center></td>
<td><center>'.$ver_name.'</center></td>
<td><center>'.$check_status.'</center></td>
<td><center>'.$final_tat.'</center></td>
<td><center><a href = "javascript: void(0);" id = "viewInfoCheckElements'.$y.'">View Elements</a></center></td>
</tr>
';
$y++;
}
?>
</tbody>
<tfoot>
<tr>
<th><center>#</center></th>
<th><center>Category</center></th>
<th><center>Name</center></th>
<th><center>Date</center></th>
<th><center>Due Date</center></th>
<th><center>Allocation</center></th>
<th><center>Status</center></th>
<th><center>TAT</center></th>
<th><center>Action</center></th>
</tr>
</tfoot>
</table>
JS
var rowCount_infoChecks = $('#tblInfoChecks >tbody >tr').length;
for(var i = 0; i < rowCount_infoChecks; i++)
{ViewInfoChecks(i);}
查看信息检查:
function ViewInfoChecks(place){
$('#viewInfoCheckElements'+place).livequery(function(){
$('#viewInfoCheckElements'+place).live("click", function(e){
var check_id = $('#txtInfoCheckID'+place).val();
alert(check_id);
var check_name = $('#txtInfoCheckName'+place).val();
var check_cat = $('#txtInfoCheckCat'+place).val();
var check_elem = $('#txtInfoCheckElements'+place).val();
var ver_status = $('#txtInfoCheckVerStatus'+place).val();
var ver_remarks = $('#txtInfoCheckVerRemarks'+place).val();
var ver_action = $('#txtInfoCheckVerAction'+place).val();
var overall_status = $('#txtInfoCheckOverallStatus'+place).val();
$.post(
"posts/view-check-elements.php",
{
check_name : check_name,
check_cat : check_cat,
check_elem : check_elem,
ver_status : ver_status,
ver_remarks : ver_remarks,
ver_action : ver_action,
overall_status : overall_status
},
function(data){
$('#popupViewInfoChecks').html(data);
$('#popupViewInfoChecks').lightbox_me({
centered: true
});
e.preventDefault();
}
);
});
});
}
每次我使用$('#').html(data);
正确加载的表格将表格加载到 div 元素时,我都会尝试取消隐藏隐藏的值并正确放置元素值。但是当我通过 a 访问它$('#').val()
并警告它的值时,它变得不一致。有时,当我访问元素时,之前选择的值是出现的值,所以我的假设是它与浏览器有关,但是当我尝试清除缓存时它仍然出现,所以我认为问题出在我的代码上。
例子:
- ID为myID0的输入选择器的值为“boy”;
- 我提醒 myID0 的值并且值男孩是正确的
- 我尝试再次加载表并查询不同的值,这样男孩就变成了女孩
- 我提醒同一选择器 myID0 的值,该值仍然是男孩,并且选择器 myID0.val() 无法识别新分配的值(女孩);