【问题标题】:Generate every row total of a table input tag in HTML在 HTML 中生成表格输入标记的每一行总计
【发布时间】:2016-11-11 06:01:06
【问题描述】:

[这种类型的输出我想要我从数据库中获取数据并存储在表中,如果用户输入的数量比我想要的总数在添加列中,用户现在将添加数量` 先生没有。 产品名称 价格 数量 添加

            </tr>
            <tr>
            <?php
            mysql_query ("set character_set_results='utf8'"); 
              $query= mysql_query("SELECT * FROM $statement LIMIT $startpoint,$limit");
              while($row = mysql_fetch_array($query))
              {


            ?>
                <td><?php echo $row['srno'];?></td>
                <td><?php echo $row['pname'];?></td>


               <!-- <td><input id="price" type="number" value="0" oninput="updateOutput()"/></td>
                <td><input id="qty" type="number" value="0" oninput="updateOutput()" /></td>-->
               <td> <input id="price" type="number" value="<?php echo $row['price'];?>" oninput="myFunction()"></td>
               <td><input id="qty" type="number"  value="0" oninput="myFunction2()" ></td>
                <td>

               <p id="demo">0</p>

                </td>


            </tr>

`]1

【问题讨论】:

  • 你在找quantity*price ??
  • 是的,我想生成总数
  • 当我使用数量选项时,总价格会发生变化吗?想用jQuery吗?
  • 是的,我试过 jQuery,但它只适用于第一行
  • 请分享您尝试过的代码...我认为您使用的是相同的 ID,只需分享代码

标签: javascript html


【解决方案1】:

<script>
function myFunction() {
    var price       = document.getElementsByClassName('price');
    var quantity    = document.getElementsByClassName('quantity');
    var totalPrice=0,totalQuantity=0;

    for(var i=0;i<price.length;i++){
        totalPrice+=parseInt(price[i].innerHTML);
    }

    for(var i=0;i<quantity.length;i++){
        totalQuantity+=parseInt(quantity[i].value);
    }

    console.log(totalPrice,totalQuantity); /*Here is your Result variable*/
	
	document.getElementById('priceResult').innerHTML = totalPrice;	
}
</script>
<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table" >
				<tr>
                 
					
                    <th class="table-header-repeat line-left minwidth-1"><a href="">Price</a></th>
                    <th class="table-header-repeat line-left minwidth-1"><a href="">Quantity</a></th>
                    <th class="table-header-repeat line-left minwidth-1"><a href="">ADD</a></th>
                   				
				</tr>
				<tr><?php
				mysql_query ("set character_set_results='utf8'"); 
				  $query= mysql_query("SELECT * FROM product");
				  while($row = mysql_fetch_array($query))
				  {
					 ?>
                
                    
                   <td class="price" >5</td>
                   <td><input class="quantity" type="number"  value="0" oninput="myFunction()" ></td>
                    <td>
                     
                   <div id="priceResult"></div>

					</td>
                 
                                       
                </tr>
<?php } ?>
</table>

【讨论】:

  • 你检查过这个@sumon sarker
  • 你不能在这段代码中运行 PHP 代码。这就是结果错误的原因。删除这个答案。我更新了答案。
【解决方案2】:

JavaScript 解决方案:

function getResult(){
    var price       = document.getElementsByClassName('price');
    var quantity    = document.getElementsByClassName('quantity');
    var totalPrice=0,totalQuantity=0;

    for(var i=0;i<price.length;i++){
        totalPrice+=parseInt(price[i].value);
    }

    for(var i=0;i<quantity.length;i++){
        totalQuantity+=parseInt(quantity[i].value);
    }

    document.getElementById('totalPrice').innerHTML = totalPrice;
    document.getElementById('totalQuantity').innerHTML = totalQuantity;
  }

getResult();
<table border="1" width="100%" cellpadding="0" cellspacing="0" id="product-table" >
    <tr style="background:#3cf;color:white">
        <th class="table-header-repeat line-left minwidth-1"><a href="">Price</a></th>
        <th class="table-header-repeat line-left minwidth-1"><a href="">Quantity</a></th>
        <th class="table-header-repeat line-left minwidth-1"><a href="">ADD</a></th>
    </tr>
    <tr>
       <td><input class="price" onkeyup="getResult()" oninput="getResult()" type="number"  value="20"></td>
       <td><input class="quantity" onkeyup="getResult()" oninput="getResult()" type="number"  value="10"></td>
       <td><p id="demo">0</p></td>
    </tr>
    <tr>
       <td><input class="price" onkeyup="getResult()" oninput="getResult()" type="number"  value="20"></td>
       <td><input class="quantity" onkeyup="getResult()" oninput="getResult()" type="number"  value="20"></td>
       <td><p id="demo">0</p></td>
    </tr>
    <tr>
       <td><input class="price" onkeyup="getResult()" oninput="getResult()" type="number"  value="50"></td>
       <td><input class="quantity" onkeyup="getResult()" oninput="getResult()" type="number"  value="30"></td>
       <td><p id="demo">0</p></td>
    </tr>
    <tr style="background:black;color:white">
        <td id="totalPrice"></td>
        <td id="totalQuantity"></td>
        <td>&nbsp;</td>
    </tr>
</table>

【讨论】:

  • 这里的数量将由用户添加
  • 所以,你可以把上面的JavaScript代码写成function,然后你可以通过调用函数@ParmarShyamsingh看到结果
  • 如何打印总计
  • 添加和 ID 示例 &lt;div id="priceResult"&gt;&lt;/div&gt; 并设置 document.getElementById('priceResult').innerHTML = totalPrice; @ParmarShyamsingh
  • 你能用例子解释一下我在打印行中放了for循环吗
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
相关资源
最近更新 更多