2013-04-18 62 views
0

我有一個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();
+1

後呈現的HTML,和/或張貼在的jsfiddle。這裏的PHP代碼不會有太大的幫助。你使用的是什麼jQuery版本? – 2013-04-18 05:47:20

+0

我無法重新創建JSFiddle上的錯誤,基本上它只會正確地獲取元素的值。問題是,我將表格加載到不同的div上,並且表格只包含一個「tblInfoChecks」ID。你認爲爲不同的實例上的表創建不同的ID會更好嗎?我正在使用jQuery 1.7.2 – 2013-04-18 05:53:06

+1

@JohnMicahFernandezMiguel,diffrent divs在同一頁?然後你檢查每個元素的ID。你不能在一個頁面中有多個具有相同ID的元素 – arjuncc 2013-04-18 05:54:17

回答

0

可能是某種問題,由於選擇器/時間來加載html。我想嘗試這樣的事:

function(data){ 
    var $popupViewInfoCheck = $('#popupViewInfoChecks'); 
    $popupViewInfoCheck.html(data); 
    $popupViewInfoCheck.lightbox_me({ 
     centered: true 
    }); 
    e.preventDefault(); 
} 

..和順便說一下,你應該總是被賦予您選擇的是變量時可能出於性能的考慮。

0

試試這個

function ViewInfoChecks(place){ 
     $('#viewInfoCheckElements'+place).livequery('click',function(){ 
       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(); 
        } 
       ); 

      }); 

    }