自动求和并附加数据javascript

时间:2015-12-20 09:13:22

标签: javascript

现在有一个来自添加更多按钮的输入,用户写了一些数字而不是他得到的总和,我试图自动得到总和。它没有工作,但我需要在文本框中输出打印....对于此代码 在这里我的javascript:

 var i = $('table tr').length;
var count = $('table tr').length;
var row = i;
var a = 0;

for (a = 0; a <= i; a++) {
  $(".addmore_" + a).on('click', function (a) {
    return function() {
    var box = $("#box_"+ a);
    box.val( Number(box.val())+1 );

      var data = "<td><input class='form-control sedang' type='text' id='packinglist_" + a + "' name='packinglist"+ a +"[]'/></td>";
      $("#keatas_" + a).append(data);

    };
  } 
  (a));
    //this calculates values automatically 
    calculateSum();

    $("#packinglist_"+a).on("keydown keyup", function() {
        calculateSum();
    });

function calculateSum() {
    var sum = 0;
    //iterate through each textboxes and add the values
    $("#packinglist_"+ a).each(function() {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum += parseFloat(this.value);
            $(this).css("background-color", "#FEFFB0");
        }
        else if (this.value.length != 0){
            $(this).css("background-color", "red");
        }
    });

    $("input#netto_"+ a).val(sum.toFixed(2));
}
}

这里是我的HTML:

       if($_GET['mod'] == 'Buat Surat Jalan Keluar'){
        function romanNumerals($num) 
{
    $n = intval($num);
    $res = '';

    /*** roman_numerals array  ***/
    $roman_numerals = array(
                'M'  => 1000,
                'CM' => 900,
                'D'  => 500,
                'CD' => 400,
                'C'  => 100,
                'XC' => 90,
                'L'  => 50,
                'XL' => 40,
                'X'  => 10,
                'IX' => 9,
                'V'  => 5,
                'IV' => 4,
                'I'  => 1);

    foreach ($roman_numerals as $roman => $number) 
    {
        /*** divide to get  matches ***/
        $matches = intval($n / $number);

        /*** assign the roman char * $matches ***/
        $res .= str_repeat($roman, $matches);

        /*** substract from the number ***/
        $n = $n % $number;
    }

    /*** return the res ***/
    return $res;
    }




echo"</div>";
echo"           

    <link rel='stylesheet' type='text/css' href='css/jquery-ui.min.css' />
    <link rel='stylesheet' type='text/css' href='css/main.css' />

";
    $month = date('n');
        $b=romanNumerals($month);
        $years=date('Y');
            echo"<div class='form-group'>";
            $tanggal=date('d'); 
                $s=mysql_query("SELECT * FROM surat_jalan WHERE identitas_surat = 1 AND bulantahun=$_POST[bulantahun] ORDER BY no_surat_jalan ASC");
                while($a=mysql_fetch_array($s)){

        $nomor = $a[2];
        $potong = (int)substr($nomor,0,3);
        $potong++;
    }
        $kosong= mysql_num_rows($s);
        if(count($kosong) == 0){

            $nomore = sprintf('%03s',1);
        }
        else {
            $nomore = sprintf('%03s',$potong);
        }


        echo"
        <body>

        <form id='students' method='post' name='students' action='../Action/tambah.php?mod=suratkeluar&batas=$_POST[jumlahdata]' autocomplete='off'>
                <div class='row '>
                    <div class='col-lg-4 '>
                        <div class='panel panel-default noborder'>
                            <div class='panel-body '>
                             <h3>No : ".$nomore."/$b/$years</h3>
                             <input class='form-control' type='hidden' id='nosurat' name='nosurat' value='".$nomore."/$b/$years' />
                            </div>
                        </div>
                    </div>
                    <div class='col-lg-4 text-center'>
                        <div class='panel panel-default'>
                            <div class='panel-body'>

                              <label>Customer</label><select name='customer' class= 'form-control '>
                                ";
                                $s=mysql_query('SELECT * FROM customer ORDER BY nama_customer ASC');
                                    while($ben=mysql_fetch_array($s)){
                                echo"<option value='$ben[id_customer]_$ben[nama_customer]_$ben[alamat_customer]'>$ben[nama_customer]</option>";
                            }

                            echo"   </select>
                            </div>
                            </div>
                        </div>

                    <div class='col-lg-4 text-center'>
                        <div class='panel panel-default'>
                            <div class='panel-body'>

                              <label>Nomor PO</label><select name='nopo' class= 'form-control '>
                              <option value='0_-'> - </option>
                                ";
                                $s=mysql_query('SELECT * FROM preorder WHERE pabrik = 0 and nonview = 0 GROUP BY no_po ORDER BY no_po ASC');
                                    while($ben=mysql_fetch_array($s)){
                                echo"<option value='$ben[id_po]_$ben[no_po]'>$ben[no_po]</option>";
                            }

                            echo"   </select>
                            </div>
                            </div>
                        </div>

                    <div class='table-responsive'>
                        <table class='table table-bordered'>
                            <tr>
                                <th>No</th>
                                <th>Jenis Benang</th>
                                <th>Warna</th>
                                <th>Lot</th>
                                <th>Netto</th>
                                <th>Box</th>
                                <th>Cones</th>
                                <th>Keterangan</th>

                            </tr>";

$s=mysql_query("SELECT * FROM surat_jalan order by id_surat DESC");
    $a=mysql_fetch_array($s);




            for($i=1;$i<=$_POST['jumlahdata'];$i++)
            {
                    $nomor = $a[0];
                    $iden = $nomor + $i;
                        echo"   <tr>
                                <td><span id='snum'>$i.</span></td>
                                <input class='form-control' type='hidden' id='hiddenlot_$i' name='hiddenlot[]' />
                                <input class='form-control' type='hidden' id='hiddencustomer_$i' name='hiddencustomer[]' />
                                <input class='form-control' type='hidden' id='hiddenprice_$i' name='hiddenprice[]' />
                                <td><input class='form-control' type='text' id='jenisbenang_$i' name='jenisbenang[]' readonly/></td>
                                <td><input class='form-control' type='text' id='warna_$i' name='warna[]' readonly/></td>                                
                                <td><input class='form-control' type='text' id='lot_$i' name='lot[]' required/></td>
                                <td><input class='form-control sedang' type='text' id='netto_$i' name='netto[]' required/> </td>
                                <td><input class='form-control pendek' type='text' id='box_$i' name='box[]' /> </td>
                                <td><input class='form-control pendek' type='text' id='cones_$i' name='cones[]'/> </td>
                                <td><input class='form-control' type='text' id='keterangan_$i' name='keterangan[]'/>
                                <input class='form-control' type='hidden' id='keterangan_$i' name='identitas[]' value='$iden'/>
                                 </td>
                            </tr>
                             <tr>
                                <td><a><span class='glyphicon glyphicon-plus addmore_$i' id='$i'></span></a><br>
                                <a><span class='glyphicon glyphicon-minus delete_$i'></a> </td>
                            <td colspan='10'>
                                <table id='keatas_$i' class='keatas'>
                                <tr>

                                </tr>   
                                </table>
                            </tr>
                            </td>";

            }   

                     echo"  

                     </table>
                     <div class='form-group'>
                        <button type='submit' class='btn btn-primary btn-lg btn-block' name='submit'>Tambah Data</button>
                    </div>

                    </div>

                </form>
          </body>
            </div>  
            </div>
        </div>  
    </div><!-- /container -->



    }

所以我的代码没有用,但其他任何工作正常

任何回应都将受到赞赏

1 个答案:

答案 0 :(得分:0)

我检查了你的代码。我在你的代码中做了一些改动。您必须在jQuery代码中使用,。这是更改"keydown, keyup"

同时更改$('body').on('click', ".addmore_" + a, function (a) {循环中的for。和$('body').on("keydown, keyup", "#packinglist_"+a, function() {

这是因为您正在创建动态输入

请检查一下,它应该有效:

        var i = $('table tr').length;
var count = $('table tr').length;
var row = i;
var a = 0;

for (a = 0; a <= i; a++) {
  $(".addmore_" + a).on('click', function (a) {
    return function() {
    var box = $("#box_"+ a);
    box.val( Number(box.val())+1 );

      var data = "<td><input class='form-control sedang' type='text' id='packinglist_" + a + "' name='packinglist"+ a +"[]'/></td>";
      $("#keatas_" + a).append(data);

    };
  } 
  (a));
    //this calculates values automatically 
    calculateSum();

    $("#packinglist_"+a).on("keydown keyup", function() {
        calculateSum();
    });

function calculateSum() {
    var sum = 0;
    //iterate through each textboxes and add the values
    $("#packinglist_"+ a).each(function() {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum += parseFloat(this.value);
            $(this).css("background-color", "#FEFFB0");
        }
        else if (this.value.length != 0){
            $(this).css("background-color", "red");
        }
    });

    $("input#netto_"+ a).val(sum.toFixed(2));
}
}

HTML代码更新:

           if($_GET['mod'] == 'Buat Surat Jalan Keluar'){
        function romanNumerals($num) 
{
    $n = intval($num);
    $res = '';

    /*** roman_numerals array  ***/
    $roman_numerals = array(
                'M'  => 1000,
                'CM' => 900,
                'D'  => 500,
                'CD' => 400,
                'C'  => 100,
                'XC' => 90,
                'L'  => 50,
                'XL' => 40,
                'X'  => 10,
                'IX' => 9,
                'V'  => 5,
                'IV' => 4,
                'I'  => 1);

    foreach ($roman_numerals as $roman => $number) 
    {
        /*** divide to get  matches ***/
        $matches = intval($n / $number);

        /*** assign the roman char * $matches ***/
        $res .= str_repeat($roman, $matches);

        /*** substract from the number ***/
        $n = $n % $number;
    }

    /*** return the res ***/
    return $res;
    }




echo"</div>";
echo"           

    <link rel='stylesheet' type='text/css' href='css/jquery-ui.min.css' />
    <link rel='stylesheet' type='text/css' href='css/main.css' />

";
    $month = date('n');
        $b=romanNumerals($month);
        $years=date('Y');
            echo"<div class='form-group'>";
            $tanggal=date('d'); 
                $s=mysql_query("SELECT * FROM surat_jalan WHERE identitas_surat = 1 AND bulantahun=$_POST[bulantahun] ORDER BY no_surat_jalan ASC");
                while($a=mysql_fetch_array($s)){

        $nomor = $a[2];
        $potong = (int)substr($nomor,0,3);
        $potong++;
    }
        $kosong= mysql_num_rows($s);
        if(count($kosong) == 0){

            $nomore = sprintf('%03s',1);
        }
        else {
            $nomore = sprintf('%03s',$potong);
        }


        echo"
        <body>

        <form id='students' method='post' name='students' action='../Action/tambah.php?mod=suratkeluar&batas=$_POST[jumlahdata]' autocomplete='off'>
                <div class='row '>
                    <div class='col-lg-4 '>
                        <div class='panel panel-default noborder'>
                            <div class='panel-body '>
                             <h3>No : ".$nomore."/$b/$years</h3>
                             <input class='form-control' type='hidden' id='nosurat' name='nosurat' value='".$nomore."/$b/$years' />
                            </div>
                        </div>
                    </div>
                    <div class='col-lg-4 text-center'>
                        <div class='panel panel-default'>
                            <div class='panel-body'>

                              <label>Customer</label><select name='customer' class= 'form-control '>
                                ";
                                $s=mysql_query('SELECT * FROM customer ORDER BY nama_customer ASC');
                                    while($ben=mysql_fetch_array($s)){
                                echo"<option value='$ben[id_customer]_$ben[nama_customer]_$ben[alamat_customer]'>$ben[nama_customer]</option>";
                            }

                            echo"   </select>
                            </div>
                            </div>
                        </div>

                    <div class='col-lg-4 text-center'>
                        <div class='panel panel-default'>
                            <div class='panel-body'>

                              <label>Nomor PO</label><select name='nopo' class= 'form-control '>
                              <option value='0_-'> - </option>
                                ";
                                $s=mysql_query('SELECT * FROM preorder WHERE pabrik = 0 and nonview = 0 GROUP BY no_po ORDER BY no_po ASC');
                                    while($ben=mysql_fetch_array($s)){
                                echo"<option value='$ben[id_po]_$ben[no_po]'>$ben[no_po]</option>";
                            }

                            echo"   </select>
                            </div>
                            </div>
                        </div>

                    <div class='table-responsive'>
                        <table class='table table-bordered'>
                            <tr>
                                <th>No</th>
                                <th>Jenis Benang</th>
                                <th>Warna</th>
                                <th>Lot</th>
                                <th>Netto</th>
                                <th>Box</th>
                                <th>Cones</th>
                                <th>Keterangan</th>

                            </tr>";

$s=mysql_query("SELECT * FROM surat_jalan order by id_surat DESC");
    $a=mysql_fetch_array($s);




            for($i=1;$i<=$_POST['jumlahdata'];$i++)
            {
                    $nomor = $a[0];
                    $iden = $nomor + $i;
                        echo"   <tr>
                                <td><span id='snum'>$i.</span></td>
                                <input class='form-control' type='hidden' id='hiddenlot_$i' name='hiddenlot[]' />
                                <input class='form-control' type='hidden' id='hiddencustomer_$i' name='hiddencustomer[]' />
                                <input class='form-control' type='hidden' id='hiddenprice_$i' name='hiddenprice[]' />
                                <td><input class='form-control' type='text' id='jenisbenang_$i' name='jenisbenang[]' readonly/></td>
                                <td><input class='form-control' type='text' id='warna_$i' name='warna[]' readonly/></td>                                
                                <td><input class='form-control' type='text' id='lot_$i' name='lot[]' required/></td>
                                <td><input class='form-control sedang' type='text' id='netto_$i' name='netto[]' required/> </td>
                                <td><input class='form-control pendek' type='text' id='box_$i' name='box[]' /> </td>
                                <td><input class='form-control pendek' type='text' id='cones_$i' name='cones[]'/> </td>
                                <td><input class='form-control' type='text' id='keterangan_$i' name='keterangan[]'/>
                                <input class='form-control' type='hidden' id='keterangan_$i' name='identitas[]' value='$iden'/>
                                 </td>
                            </tr>
                             <tr>
                                <td><a><span class='glyphicon glyphicon-plus addmore_$i' id='$i'></span></a><br>
                                <a><span class='glyphicon glyphicon-minus delete_$i'></a> </td>
                            <td colspan='10'>
                                <table id='keatas_$i' class='keatas'>
                                <tr>

                                </tr>   
                                </table>
                            </tr>
                            </td>";

            }   

                     echo"  

                     </table>
                     <div class='form-group'>
                        <button type='submit' class='btn btn-primary btn-lg btn-block' name='submit'>Tambah Data</button>
                    </div>

                    </div>

                </form>
          </body>
            </div>  
            </div>
        </div>  
    </div><!-- /container -->";



    }