简体   繁体   English

如何从循环输入类型中减去总数

[英]How to deduct a total number from a looped input type=number

Here's my code: 这是我的代码:

<html>

<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.js"></script>
<script type="text/javascript">//<![CDATA[ 
$(window).load(function(){
$(".sub").focusout(function() {
    $("#answer").html('');
    var num = $("#num").val();
    var answer = 100 - num;
    $("#answer").html(answer);
});
});//]]>  
</script>
</head>

<body>
<p id='answer'> 100 </p>
<?php
    $db=mysql_connect("localhost","root","") or die("Could not connect to database");
    mysql_select_db("cb_client_database") or die("could not select database");
    $query = "SELECT * FROM ra_rooms WHERE Category = 'Double Deluxe Room'";
    $result=mysql_query($query, $db) or die(mysql_error($db));

    echo "<table>";

    while ($row = mysql_fetch_array($result))
    {
    extract($row);
?>
<tr><td><input type='number' max='<?phpecho"$Min_Capacity";?>' min='0' name='num' id=num class=sub><?phpecho"$RoomNumber";?></td></tr>


 <?php
     } 
     echo "</table>";
 ?>
</body>
</html>

For example: If I have four(4) records(This depends on how many records my database have), it should generate 3 more variables so the calculation should be like this: 例如:如果我有四(4)条记录(这取决于我的数据库有多少条记录),它应该再生成3个变量,因此计算应如下所示:

 var answer = 100 - (num + num2 + num3 + num4);

The problem is, I don't know how to. 问题是,我不知道该怎么办。 Please help me. 请帮我。

First, remove id="num" from the <input> elements (you can't have more than one unique identifier per document). 首先,从<input>元素中删除id="num" (每个文档最多只能有一个唯一标识符)。

Then, update your code a little to calculate the sum over multiple elements: 然后,稍微更新代码以计算多个元素的总和:

var num = 0;
$('input.sub').each(function() {
    num += parseInt(this.value, 10);
});
var answer = 100 - num;
$("#answer").html(answer);

I've made the assumption that "input.sub" matches the right input elements. 我已经假设"input.sub"与正确的输入元素匹配。

1) write echo"$RoomNumber"; 1)写回显“ $ RoomNumber”; in value of input. 输入值。

2)use double quotes for id and class input. 2)使用双引号输入id和class。

3.try with counter check below php code .id of input will increase auto. 3.尝试与下面的PHP代码计数器检查输入的.id将自动增加。

 <script>  

  var num1 = $('#num1').val();  
    var num2 = $('#num2').val();  
    var num3 = $('#num3').val();  
var num4 = $('#num4').val();  
    var answer = 100 - (num1+num2+num3+num4 );
    </script>  

<?php   
    $x=1; 
    while($x<=4)   //4 or count($result)
    {

    ?>  
<tr><td><input type='number'  min='0' name='num' value="<?phpecho"$RoomNumber";?>" id="num<?php echo $x;?>" class="sub"></td></tr>

<?php
$x=$x+1;  

}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从输出的数字中减去%并显示一个新数字 - Deduct % from outputted number and display a new number 如何动态限制重复值<input='text' />在一个循环的随机值中<input='number' />场地 - How to dynamically Restrict Duplicated value of <input='text'/> in a looped random value from <input='number'/> field Jquery:如何根据输入类型 = 数字中的值计算每个 function 的总平均值? - Jquery: How can I compute total average of each function based from the values in the input type = number? 如何从输入框中减去总数的数字? - How to subtract number from the input box for Grand Total? 从数字输入计算总和并设置为数字输入 - Calculate total from number input and set to number input 如何从输入类型=数字中获取值? - How to get value from input type = number? 如何从数字中扣除一个百分比,然后使用Javascript或Jquery将差异分配给innerHTML属性? - How do i deduct a percentage from a number and assign the difference to the innerHTML attribute using Javascript or Jquery? 如何在输入框中显示数量总数 - How to display the total number of quantity in the input box 如何将数字输入保持为数字数据类型 - how to keep number input as number data type html输入type = number“可用”值中的更改。 还将更改另一种输入类型=数字“总计” - html Change in input type=number “available” value. will also change another input type=number “Total”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM