jQuery后更新/更改表单元格文本

时间:2018-10-29 08:32:13

标签: javascript php jquery html

我想为每次更改的开关更改签入时间。我的问题是,当我更改其中任何一个时,我只会更改第一行签入时间。 如果有人可以帮助解决我的问题。

更新:想要更改签入时间,当我更改第一行时,第一行签入时间将被更新或给出文本,依此类推... 2nd开关更改第二行签入时间... 3rd ..等等。谢谢。

<script>
$(document).ready(function () {
    $('.checkin-button').click(function(e){                           
        if ($(this).is(':checked')){
            var famid =  $(this).closest('tr').find('input[name="famno"]').val();
            var a = this.checked ? 1 : 0;
            $.ajax({
                type: 'POST',
                url: 'checkdep.php',
                data: {famid: famid}
            }).done(function(res){
                console.log(res);
                if (res == 1 ){
                    var date = //the date **wanted to pass date to TIMEEMP. Since I still dont know how to and how to give value to the var with dd/mm/yyyy HH:mm:ss a format.
                    $('timeemp').text/html(date) **confused here
                }else{
                    alert('Check-in failed');
                }
            });
        }else{
    });
</script>




  <html>
            <tr>

           <td><?php echo $no; ?></td>
            <td><?php echo $company; ?></td>
            <td><?php echo $branch; ?></td>
            <td><a href="empmain.php?access=view&photos=<?php echo $photos;?>&lname=<?php echo $lname; ?>&fname=<?php echo $fname; ?>&empid=<?php echo $empid;?>&empno=<?php echo $empno;?>"><span class="glyphicon glyphicon-user" aria-hidden="true"></span><?php echo $empid;?></a></td>

            <td><?php echo $depthead; ?></td>
            <td id="timeemp"><?php echo $timecheck; ?></td>
            <td>

            <div class = "form-group">

                    <form id="empform" action="" method="post">

                            <?php
                                $userid='1';
                             if($userid == '1'){

                                       if ($row['Status'] == ''){
                                        echo'
                                            <div class="form-group">
                                                <div class="col-md-12 col-sm-12 col-xs-12">
                                                    <div class="">
                                                        <label>

                                                            <input type="checkbox" class="js-switch checkemp-button" />
                                                            <input name="emp" value = "'.$empno.'" type="hidden" />
                                                        </label>

                                                    </div>
                                                </div>
                                            </div>';
                                        }
                                    else{

                                            echo '<div class="form-group">
                                            <div class="col-md-9 col-sm-9 col-xs-12">
                                                <div class="">
                                                    <label>

                                                            <input type="checkbox"  class="js-switch checkemp-button" checked />
                                                            <input name="emp" value = "'.$empno.'" type="hidden" />
                                                    </label>
                                                </div>
                                            </div>
                                        </div>';
                                    }
                                   }
                                   else{

                                   }

                            ?>

                    </form>

            </div>
            </td>

        </tr>
   </html>

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试以下更新的代码。

$('.checkin-button').click(function(e){                           
        if ($(this).is(':checked')){
            var famid =  $(this).closest('tr').find('input[name="famno"]').val();
            var a = this.checked ? 1 : 0;
            var selectedRow = $(this).closest('tr'); // Updated
            $.ajax({
                type: 'POST',
                url: 'checkdep.php',
                data: {famid: famid}
            }).done(function(res){
                console.log(res);
                if (res == 1 ){
                    var date = //the date **wanted to pass date to TIMEEMP. Since I still dont know how to and how to give value to the var with dd/mm/yyyy HH:mm:ss a format.
                    selectedRow.find('#timeemp').text(date); // Updated
                }else{
                    alert('Check-in failed');
                }
            });
        }else{}
    });

答案 1 :(得分:0)

如果要使用当前系统时间,则可以通过“ date = new Date()”获得。 但是默认情况下,您将获得完整的日期字符串。如果您需要将其转换为自定义格式,则可以采用以下任何一种方式

1)您可以使用moment.js =>这将有助于以自定义格式获取日期,并且还可以根据时区进行转换

2)您可以通过以下功能使用javascript获取日期(如给定格式):

function getDateString(){
    var currentDate = new Date();
    var month = currentDate.getMonth() + 1;
    if(month<=9)    
        month="0" +month;
    var date = currentDate.getDate();
    if(date<=9) 
        date="0" +date;
    var hours = currentDate.getHours();
    var meridian = "AM";
    if(hours>=12){
        hours= hours % 12;
        meridian = "PM"
    }
    if(hours<=9){
        hours = "0"+hours;
    }   
    var minutes = currentDate.getMinutes();
    if(minutes<=9)  
        minutes="0" +minutes;
    var seconds = currentDate.getSeconds();
    if(seconds<=9)  
        seconds="0" +seconds;   
    return month+"/"+date+"/"+currentDate.getFullYear()+" "+hours+":"+minutes+":"+seconds+ " "+meridian;
}