jQuery每个函数结果怎么办?

时间:2018-10-24 12:21:37

标签: javascript php jquery mysql

我必须构建一个表单,并且我一直在使用jQuery,但是如果您能给我任何建议,我会陷入第一步,

所以,让我解释一下我有20种产品的形式,其中有offer1,offer2和月费,到目前为止,所有这些都可以正常使用,我只需要在选择了特定产品的数量之后,然后按下一个按钮再次显示结果,但仅显示填充量,因此基本上只显示客户对它们感兴趣的产品,这是我的主要问题,我知道我必须在jQuery中执行每个函数,但是我不确定该怎么做它吗?

我在表格上附加了图片,因此您可以看到一个示例 form picture

编辑: 这是我目前的代码:

    function convert(value) {
        return "" + ((Number(value) || 0).toFixed(2).replace(/(\d)(?=(\d{3})+ 
        (?!\d))/g, "$1,")) + "";
    }

    function testing(id, normaloffer, talesalesoffer, montlyoffer) {
        var normaoffertotal = normaloffer * $('#amount' + id).val();
        var talesalestotal = talesalesoffer * $('#amount' + id).val();
        var montlytotal = montlyoffer * $('#amount' + id).val();
        $(".normaloffertotal" + id).html(normaoffertotal);
        $(".telesalesoffertotal" + id).html(talesalestotal);
        $(".montlytotal" + id).html(montlytotal);
        $('#hnoffer' + id).val(normaoffertotal);
    }

    console.log(convert(10496.470000000001));

    function normaloffertotalcalc() {
        var sumnormaltotal = 0;
        $('span[id^="normaloffertotalspan"]').each(function () {
            var text = $(this).text();
            sumnormaltotal += parseFloat(text, 10);
        });
        $('#sumnormaltotal').html(sumnormaltotal);

        var sum_telesale = 0;
        $('span[id^="telesalesoffertotalspan"]').each(function () {
            var text = $(this).text();
            sum_telesale += parseFloat(text, 10);
        });
        $('#sumtelesaletotal').html(sum_telesale);

        var sum_montly = 0;
        $('span[id^="montlyofferspan"]').each(function () {
            var text = $(this).text();
            sum_montly += parseFloat(text, 10);
        });
        $('#summontlytotal').html(sum_montly);

        console.log('Normal Total Offer ' + convert(sum_normal));
        console.log('TeleSales Total Offer ' + convert(sum_telesale));
        console.log('Montly Total Offer ' + convert(sum_montly));
    }
</script>

这是html

<?php
    $count = 0;
    $query = mysqli_query($con, "SELECT * FROM products");
    while ($row = mysqli_fetch_array($query)) {
    $count++;
?>
<tr>
    <td align="center"><?= $row[1] ?></td>
    <td align="center"><input type="text" placeholder="0" id="amount<?= $count ?>"
                                              name="amount<?= $count ?>" onblur="normaloffertotalcalc()"
                                              onkeyup="testing(<?= $count ?>, <?= $row[2] ?>, <?= $row[3] ?>, <?= $row[4] ?>)"
                                              style="-webkit-appearance: none; background: none; text-align: center; border: none; width: 40px;">
     </td>
     <td align="center">£<?= $row[2] ?></td>
     <td align="center">£<span class="normaloffertotal<?= $count ?>"
                                              id="normaloffertotalspan">0</span> <input type="hidden"
                                                                                        class="normaloffertotalspan"
                                                                                        id="hnoffer<?= $count ?>">
      </td>
      <td align="center">£<?= $row[3] ?></td>
      <td align="center">£<span class="telesalesoffertotal<?= $count ?>" id="telesalesoffertotalspan">0</span>
      </td>
      <td align="center">£<?= $row[4] ?></td>
      <td align="center">£<span class="montlytotal<?= $count ?>" id="montlyofferspan">0</span></td>
                </tr>
      <?php } ?>

1 个答案:

答案 0 :(得分:0)

我为此问题找到了解决方案

            $('input[type=text]').each(function(){
            var val = parseInt($(this).val());

            if(val > 0){
                var id = $(this).attr('id');
            if(id){
                console.log(id);
            }
                console.log(val);

            }
        });

这是我所做的代码,并且运行良好:)

感谢所有给我建议的人

还有一件事情,您是否知道我该如何从MySQL中仅导出ID为2,7,1的产品:)