我有一个JavaScript函数处理表中的动态ID。jQuery val()返回以前选择的元素值
表:
<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);}
ViewInfoChecks:
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元素,我要尽量取消隐藏隐藏值和元素值被正确放置。但是,当我通过$('#').val()
访问它并提醒它的值变得不一致时。有时当我访问元素时,前面选择的值是出现的那个,所以我的假设是它与浏览器有关,但是当我尝试清除缓存时,它仍然出现,所以我认为问题是我的代码。
实施例:
- 与ID myID0输入选择器的值是 “男孩”;
- 我提醒myID0的价值和价值男孩是正确的
- 我尝试让男孩 变成女孩
- 我提醒同一选择myID0,该值的价值,再次加载表和查询不同的值仍然是男孩 并且新分配的值(女孩)未被选择器 识别myID0.val();
后呈现的HTML,和/或张贴在的jsfiddle。这里的PHP代码不会有太大的帮助。你使用的是什么jQuery版本? – 2013-04-18 05:47:20
我无法重新创建JSFiddle上的错误,基本上它只会正确地获取元素的值。问题是,我将表格加载到不同的div上,并且表格只包含一个“tblInfoChecks”ID。你认为为不同的实例上的表创建不同的ID会更好吗?我正在使用jQuery 1.7.2 – 2013-04-18 05:53:06
@JohnMicahFernandezMiguel,diffrent divs在同一页?然后你检查每个元素的ID。你不能在一个页面中有多个具有相同ID的元素 – arjuncc 2013-04-18 05:54:17