表td中的jQuery类和输入

时间:2017-03-08 08:33:30

标签: jquery class input html-table

我想选择带有类的输入字段的值,并使用带有类的另一个输入字段的值来计算它。它们都嵌套在一个表中。我试图调用codeAddress();进行计算

请查看代码

<script type="text/javascript">
$(document).ready(function()
{
    var counter =1;
    var str1;
    var str2;
    var x;
    var grand_total=0;
    $("#btn1").click(function()
    {
        var table = document.getElementById("myTable");
        var row = table.insertRow();

        var cell0 = row.insertCell(0);
        var cell1 = row.insertCell(1);
        var cell2 = row.insertCell(2);
        var cell3 = row.insertCell(3);
        var cell4 = row.insertCell(4);
        var cell5 = row.insertCell(5);
        var cell6 = row.insertCell(6);
        var cell7 = row.insertCell(7);
        var cell8 = row.insertCell(8);
        var cell9 = row.insertCell(9);
        var cell10 = row.insertCell(10);
        var cell11 = row.insertCell(11);
        var cell12 = row.insertCell(12);
        var cell13 = row.insertCell(13);
        var cell14 = row.insertCell(14);

        cell0.innerHTML = counter;

        <?php
        $smt = $pdo->prepare("select id as mid, Name from manufacturer");
        $smt->execute();
        $data = $smt->fetchAll();?>
        str1 = "ManufacturerID";
        str2 = str1.concat(counter);
        cell1.innerHTML = "<select name='"+str2+"' id='"+str2+"'>";
        <?php foreach ($data as $row): ?>
        var opt = document.createElement('option');
        opt.value = "<?php echo $row['mid'];?>";
        opt.innerHTML = "<?php echo $row["Name"];?>";
        document.getElementById(str2).appendChild(opt);
        <?php endforeach ?>
        document.getElementById(str2).append ="</select>";

        <?php
        $smt = $pdo->prepare("select ID, Descr, OriginalCode FROM consumables_model");
        $smt->execute();
        $data = $smt->fetchAll();?>
        str1 = "ConsumablesModelID";
        str2 = str1.concat(counter);
        cell2.innerHTML = "<select name='"+str2+"' id='"+str2+"'>";
        <?php foreach ($data as $row): ?>
        opt = document.createElement('option');
        opt.value = "<?php echo $row['ID'];?>";
        opt.innerHTML = "<?php echo $row["Descr"]."-".$row["OriginalCode"];?>";
        document.getElementById(str2).appendChild(opt);
        <?php endforeach ?>
        document.getElementById(str2).append ="</select>";

        <?php
        $smt = $pdo->prepare("select ct.ID as ctid, ct.Descr as ctDescr from consumables_type as ct");
        $smt->execute();
        $data = $smt->fetchAll();?>
        str1 = "ConsumableTypeID";
        str2 = str1.concat(counter);
        cell3.innerHTML = "<select name='"+str2+"' id='"+str2+"'>";
        <?php foreach ($data as $row): ?>
        opt = document.createElement('option');
        opt.value = "<?php echo $row['ctid'];?>";
        opt.innerHTML = "<?php echo $row['ctDescr'];?>";
        document.getElementById(str2).appendChild(opt);
        <?php endforeach ?>
        document.getElementById(str2).append ="</select>";

        <?php $smt = $pdo->prepare("select am.id as id, concat(description,' ',m.descr,' ',am.descr) as Descr from asset_type inner join asset_model am on am.assettypeid = asset_type.id inner join manufacturer m on m.id = am.manufacturerid");
        $smt->execute();
        $data = $smt->fetchAll();?>
        str1 = "AssetModelID";
        str2 = str1.concat(counter);
        cell4.innerHTML = "<select name='"+str2+"' id='"+str2+"'>";
        <?php foreach ($data as $row): ?>
        opt = document.createElement('option');
        opt.value = "<?php echo $row['id'];?>";
        opt.innerHTML = "<?php echo $row['Descr'];?>";
        document.getElementById(str2).appendChild(opt);
        <?php endforeach ?>
        document.getElementById(str2).append ="</select>";

        str1 = "Original";
        str2 = str1.concat(counter);
        cell6.innerHTML = "<select name='"+str2+"' id='"+str2+"'>";

        opt = document.createElement('option');
        opt.value = "ANAK";
        opt.innerHTML = "ANAK";

        opt2 = document.createElement('option');
        opt2.value = "ΓΝΗΣΙΟ";
        opt2.innerHTML = "ΓΝΗΣΙΟ";

        document.getElementById(str2).appendChild(opt);
        document.getElementById(str2).appendChild(opt2);
        document.getElementById(str2).append ="</select>";

        str1 = "Items";
        var tmx = str1.concat(counter);
        x="#".concat(str1).concat(counter);
        cell7.innerHTML = "<input name='"+tmx+"' id='"+tmx+"' class='qty'>";

        <?php 
        $smt = $pdo->prepare("SELECT id, name from vendor");
        $smt->execute();
        $data = $smt->fetchAll();?>
        str15 = "VendorID";
        str2 = str15.concat(counter);
        cell8.innerHTML = "<select name='"+str2+"' id='"+str2+"'>";
        <?php foreach ($data as $row): ?>
        opt = document.createElement('option');
        opt.value = "<?php echo $row['id'];?>";
        opt.innerHTML = "<?php echo $row['name'];?>";
        document.getElementById(str2).appendChild(opt);
        <?php endforeach ?>
        document.getElementById(str2).append ="</select>";

        var str3 = "ValueperUnit";
        var str4 = str3.concat(counter);
        var str5 = "#".concat(str3).concat(counter);
        cell10.innerHTML = "<input name='"+str4+"' id='"+str4+"' class='unit'>";

        var str6 = "Subtotal";
        var str7 = str6.concat(counter);
        var str8 = "#".concat(str6).concat(counter);
        cell11.innerHTML = "<input name='"+str7+"' id='"+str7+"' value='' readonly >";

        var str9 = "vat";
        var str10 = str9.concat(counter);
        var str11 = "#".concat(str9).concat(counter);
        cell12.innerHTML = "<input name='"+str10+"' id='"+str10+"' value='' readonly >";

        var str12 = "Linetotal";
        var str13 = str12.concat(counter);
        var str14 = "#".concat(str12).concat(counter);
        cell13.innerHTML = "<input name='"+str13+"' id='"+str13+"' class='line' value='' readonly >";

        <?php Database::disconnect();?> 
        counter++;
        codeAddress();
    });

    function codeAddress() 
    {
        $(".qty").keyup(function(){
        $(this).closest('tr').find("input").each(function() {
            var tem = $(this);
            var temaxia = tem.val();
            var price = tem.next().next().val();
            var sub = tem.next().next().next().val();
            var vat = tem.next().next().next().next().val();
            var line = tem.next().next().next().next().next().val();

            tem.next().next().next().val(price * tem.val());//Subtotal
            tem.next().next().next().next().val(price * tem.val()*0.24);//Vat
            tem.next().next().next().next().next().val(price * tem.val()+price * tem.val()*0.24);//Line
            fnAlltotal();
            });
        });

        //$(".unit").on('input', function () {  
        $(".unit").keyup(function(){
        $(this).closest('tr').find("input").each(function() {
            var p = $(this);
            var price = p.val();
            var temaxia = p.prev().prev().val();
            var sub = p.next().val();
            var vat = p.next().next().val();
            var line = p.next().next().next().val();

            p.next().val(temaxia * p.val());//Subtotal
            p.next().next().val(temaxia * p.val()*0.24);//Vat
            p.next().next().next().val(temaxia * p.val()+temaxia * p.val()*0.24);//Line 
            fnAlltotal();
            });
        });

        function fnAlltotal(){
            var total=0
            $(".line").each(function(){
            total += parseFloat($(this).val()||0);
            });
            $("#result").val(total);
        }
    }
}); 
</script>

<body>
<?php
//include('header.php');
?>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <h3>Παραγγελία Αναλωσίμων</h3>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <table style="width:100%" class="table table-striped table-bordered" id="myTable">
                <thead>
                    <tr>
                    <th>Α/Α</th>
                    <th>Κατασ/τής</th>
                    <th>OriginalCode</th>
                    <th>Τύπος Αν</th>
                    <th>Τύπος Εκτυπωτή</th>
                    <th>Διάρκ Γνήσιου</th>
                    <th>Γνήσιο</th>
                    <th>ΤΜΧ</th>
                    <th>ΚΑΤ ΠΡΟΣΦ</th>
                    <th>ΔΙΑΡΚ ΠΡΟΣΦ</th>
                    <th>ΤΙΜΗ ΜΟΝ</th>
                    <th>ΜΕΡ ΣΥΝΟΛΟ</th>
                    <th>ΦΠΑ</th>
                    <th>ΤΙΜΗ ΜΕ ΦΠΑ</th>
                    <th><button id="btn1">Προσθήκη</button></th>
                    </tr>
                </thead>
                <tbody>
                    <tr></tr>
                </tbody>
            </table>
        </div>
        <div class="row">
            <div class="col-sm-2">
                <p id="result"></p>
            </div>
        </div>
    </div>
</div>

<?php
    include('../footer.php');
?>
  </body>
</html>

我无法在fnAlltotal函数中进行计算。

0 个答案:

没有答案