jQuery val()返回先前选择的元素值

时间:2013-04-18 05:39:16

标签: php jquery

我有一个javascript函数,可以处理表中的动态ID。

TABLE:

<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的输入选择器的值为&#34; boy&#34;;
  • 我警告myID0的值,而值男孩是正确的
  • 我尝试再次加载表并查询不同的值,以便男孩 成为女孩
  • 我警告同一个选择器myID0的值,值仍然是男孩 并且选择器无法识别新分配的值(女孩) myID0.val();

2 个答案:

答案 0 :(得分:0)

由于加载html的选择器/时间,可能会出现某种问题。我会尝试这样的事情:

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

..顺便说一下,出于性能原因,您应该尽可能将选择器分配给变量。

答案 1 :(得分: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();
                    }
                );

            });

    }
相关问题