【问题标题】:How to make input affect an echo如何使输入影响回声
【发布时间】:2020-03-21 19:29:41
【问题描述】:

在这个问题中,当我用按钮将一个数字与数量相加时,我想改变总数但它没有改变,这是这一行:

<td width="20%" class="Text-center"><?php echo number_format($producto['PRECIO']*$producto['CANTIDAD'],2);  ?></td>

这是php代码

  <tr>
        <td width="40%"><?php echo $producto['NOMBRE'] ?></td>
        <td><button onclick="add_number()">mas</button><input type="text" id="suma" name="suma" value="<?php echo $producto['CANTIDAD'] ?>" readonly="readonly"></input></td>
        <td width="20%" class="Text-center"><?php echo $producto['PRECIO'] ?></td>
        <td width="20%" class="Text-center"><?php echo number_format($producto['PRECIO']*$producto['CANTIDAD'],2);  ?></td>
        <td width="5%">
            <form action="" method="post">
                <input type="hidden" 
                name="id" 
                value="<?php echo openssl_encrypt($producto['ID'],COD,KEY); ?>">



                <button 
                class="btn btn-danger" 
                type="submit"
                name="btnAccion"
                value="Eliminar"
                >Eliminar</button>

            </form>

    </td>
    </tr>

这是 Java 代码:

function add_number() {

    var first_number = parseInt(document.getElementById("suma").value);
    var result = first_number + 1;

    document.getElementById("suma").value = result

}

【问题讨论】:

  • Java 之于 Javascript 就像 Pain 之于绘画,或者 Ham 之于仓鼠。它们完全不同。强烈建议有抱负的编码人员尝试学习他们尝试编写代码的语言的名称。当您发布问题时,请适当地标记它 - 这可以让那些了解您需要帮助的语言的人看到您的问题。

标签: javascript java php html


【解决方案1】:

在增加数量值的函数add_number中,您还可以每次将价格添加到表格的总单元格中,像这样更改您的JavaScript函数:

function add_number() {
    var suma = parseInt(document.getElementById("suma").value); /* get current quantity */
    var total = parseInt(document.getElementById("total").innerHTML); /* get current total */
    var percio = parseInt(document.getElementById("percio").innerHTML); /* get price */

    document.getElementById("suma").value = suma + 1; /* change quantity */
    document.getElementById("total").innerHTML = (suma + percio).toFixed(2); /* change total */

}

注意innerHTMLvalue 的用法。区别在于value 用于&lt;input&gt; 标签,而innerHTML 用于&lt;td&gt; 标签。

另请注意,您需要为总单元格和价格单元格设置 id,以便在 JavaScript 函数中读取和写入它们。

<td width="20%" class="Text-center" id="percio">
    <?php echo $producto['PRECIO'] ?>
</td>
<td width="20%" class="Text-center" id="total">
    <?php echo number_format($producto['PRECIO']*$producto['CANTIDAD'],2);  ?>
</td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-04
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多