简体   繁体   English

在 Jquery 之后更新/更改表格单元格文本

[英]Updating/changing table cell Text after Jquery

I wanted to change the Checkin time for every switch I change.我想为我更改的每个开关更改签入时间。 My problem is that when I change any of those, I will only change the 1st row checkin time.我的问题是,当我更改其中任何一个时,我只会更改第一行的签到时间。 If anyone could help with the problem I have.如果有人可以帮助解决我遇到的问题。

Update: Wanted to change the checkin time, when I change the first row, the first row checkin time will be updated or given text and so on... 2nd switch changes the 2nd row checkin time ... 3rd.. and so on.更新:想要更改签到时间,当我更改第一行时,第一行签到时间将被更新或给出文本等...第二个开关更改第二行签到时间......第三...依此类推. Thanks.谢谢。

<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>

在此处输入图片说明

Try the following updated code.尝试以下更新的代码。

$('.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{}
    });

If you want to use the current system time you can get it by "date = new Date()".如果要使用当前系统时间,可以通过“date = new Date()”获取。 But by default you will get the full date String.但默认情况下,您将获得完整的日期字符串。 If you need to convert it into custom format you can follow any of the following ways如果您需要将其转换为自定义格式,您可以按照以下任何一种方式

1) You can use moment.js => this will be helpful to get date in custom format and also you can convert based on the time zone as well 1) 您可以使用 moment.js => 这将有助于以自定义格式获取日期,您也可以根据时区进行转换

2) You can get date(as like the given format) using javascript by the following function 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM