[英]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.