简体   繁体   English

自动在表的每一行中重复jquery脚本

[英]Repeating jquery script in every row in table automatically

I have problem with this script. 我对此脚本有疑问。 When I change value in column "TO" in table, script substract automatically in column "Number of hours" only in first row value but others no. 当我更改表中“ TO”列中的值时,脚本仅在第一行值中自动在“小时数”列中减去,而其他则没有。 I need application script with name "PRO1" (updateDue) in every row in table. 我需要在表的每一行中使用名称为“ PRO1”(updateDue)的应用程序脚本。

<meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css">
        <body onload="changeText()">
            <?php
                $date = date('Y-m-01');
                $end = date('Y-m-t');
                //, strtotime('-1 months')//
            ?>
            <?php include 'dbconfig.php' ?>
            <table border="1" id="myTable">
                <tr>
                    <td>Date</td>
                    <td>From</td>
                    <td>To</td>
                    <td>Number of hours</td>
                </tr>
                <tr class="item">
                    <?php
                        date_default_timezone_set('UTC');
                       while(strtotime($date) <= strtotime($end)) {     
                           $day_num = date('d', strtotime($date));
                           $day_name= date('l', strtotime($date));
                           $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
                           if (($day_name == "Saturday")||($day_name == "Sunday"))
                           {
                               echo "<td id='color'>$day_num $day_name</td><td></td><td></td><td></td></tr>";
                           }    
                           else { 
                               echo "<td>$day_num $day_name</td><td>";
                               $query = mysql_query("SELECT * FROM norma") or die(mysql_error());
                               while($query_row = mysql_fetch_assoc ($query))
                               {
                                   $starter= $query_row['start'];
                                   echo "<input type='text' class='txtCal' id='num1' onchange='updateDue()' value=".$query_row['start'].">";
                               }
                               echo "</td><td>";
                               $query = mysql_query("SELECT * FROM norma") or die(mysql_error());
                               while($query_row = mysql_fetch_assoc ($query))
                               {
                                   $finisher= $query_row['end'];
                                   echo "<input type='text' class='txtCal' id='num2' onchange='updateDue()' value=".$query_row['end'].">";
                               }
                               echo "</td><td>";
                               $ts1 = strtotime($starter);
                               $ts2 = strtotime($finisher);     
                               $seconds_diff = $ts2 - $ts1;                            
                               $time = ($seconds_diff/3600);
                               echo "<input type='text' class='txtCal2' id='remainingval' value=".number_format((float)$time, 2, '.', '').">";
                               echo "</td></tr>";
                           }    
                       }
                   ?>
               </tr>
               <tr>
                   <td></td>
                   <td></td>
                   <td>Sum:</td>
                   <td id="total_sum_value"><span id="total_sum_value"></span></td>
               </tr>
           </table>
           <br>
<!-- SCRIPT NAME PRO1 -->
<script>
    function updateDue() {
        $('#myTable tr').each(function(){
            var total = parseFloat(document.getElementById("num2").value);
            var val2 = parseFloat(document.getElementById("num1").value);
            // to make sure that they are numbers
            if (!total) { total = 0; }
            if (!val2) { val2 = 0; }
            var ansD = document.getElementById("remainingval");
            ansD.value = total - val2;
       });
    }
</script>
<script>
    $(document).ready(function changeText(){
        $(document).ready(function changeText(){
            var calculated_total_sum = 0;

            $("#myTable .txtCal2").each(function () {
                var get_textbox_value = $(this).val();
                calculated_total_sum += parseFloat(get_textbox_value);                 
            });
            $("#total_sum_value").html(calculated_total_sum);
        }); 
        $("#myTable").on('change','input', '.txtCal2', function () {
            var calculated_total_sum = 0;

            $("#myTable .txtCal2").each(function () {
                var get_textbox_value = $(this).val();
                if ($.isNumeric(get_textbox_value)) {
                    calculated_total_sum += parseFloat(get_textbox_value);  
                }             
            });
            $("#total_sum_value").html(calculated_total_sum);
        }); 
    });
</script>

I want dynamically change table. 我要动态更改表。 In column "FROM" and "TO" is value from database. 在“ FROM”和“ TO”列中是数据库中的值。 This automatically subtract value (TO-FROM) and sum from this value is down. 这将自动减去值(TO-FROM),并且该值的和减少。 But when user change some value in column "FROM" or "TO", number will be count automatically. 但是,当用户在“ FROM”或“ TO”列中更改某些值时,数字将自动计数。 Thank you so much for every advice. 非常感谢您的每条建议。

That's because you use the same ID for multiples TDs. 那是因为您对多个TD使用相同的ID。 You can't do that as an ID is supposed to be used once and help to identify a resource. 您不能这样做,因为ID应该被使用一次并有助于识别资源。

instead of an ID, you could use a class : 可以使用class代替ID:

<table>
    ...
    <tr>
        <td><input type="text" class="txtCal num1" value="5" /></td>
        <td><input type="text" class="txtCal num2" value="10" /></td>
        <td><input type="text" class="txtCal2 remainingval" value="...">
    </tr>
    ...
</table>

Then with jQuery, you can have access to your row values like this : 然后使用jQuery,您可以像这样访问行值:

$('#myTable tr').each(function(){
    var total = parseFloat($(this).find('.num2').val());
    var val2 = parseFloat($(this).find('.num1').val());

    ...
    var ansD = $(this).find(".remainingval");
    ansD.val(total - val2);
});

Also let me point that mysql functions are deprecated and you should not use them anymore. 还请允许我指出mysql函数已被弃用,您不应再使用它们。 Use PDO instead. 请改用PDO。 (See Why shouldn't I use mysql_* functions in PHP? ) (请参阅为什么我不应该在PHP中使用mysql_ *函数?

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

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