如何用javascript求和多个(未定义的数字)文本框的数量
How to sum the numbers of multiple (undefined number) textboxes with javascript?
我正在创建一个表与php和html从数据库的值,表的字段是textfield。所有的文本字段从第一列有相同的名称,所有从第二列有相同的名称…等。
在第5列之后,有一个空列,我应该显示该行第5个文本字段的总和。所有的行都是不同的形式。我不知道如何做到这一点,我甚至不知道是否有一个简单的方法来选择文本字段的形式,所以任何帮助将是伟大的。
前5个文本字段的总和应该出现在空列中,当任何文本框的值被改变时,javascript函数应该被触发。如果有人知道怎么做,那就太好了。
如果您想查看表创建代码:
<?php
$arrayida=array(0);
$arrayidp=array(0);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="3Ciclo"; // Database name
// Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("Cannot select Database.");
$queryida="SELECT * FROM areas where tipo='AE' or tipo='C'";
$resultida=mysql_query($queryida);
while($b = mysql_fetch_array($resultida))
{
$ida=$b["ida"];
if (in_array ($ida , $arrayida ))
{
}else{
$arrayida[]=$ida;
/*while($b = mysql_fetch_array($resultida))
{
?>
<td> <?php echo $ida." - ".$b['nome']; ?> </td>
<?php
}*/
}
}
sort($arrayida);
//print_r($arrayida);
foreach ($arrayida as $key => $val)
{
if($key <> 0)
{
if ($key == 5){
$prev=$val;
$queryida="SELECT * FROM areas where ida=".$val;
$resultida=mysql_query($queryida);
while($b = mysql_fetch_array($resultida))
{
$nome=$b["nome"];
$idz=$b["ida"];
?> <td style="font-size:9px"> <?php echo "$nome"; ?> </td> <td style="background-color:#FFF"> </td> <?php
}
}else
{
$queryida="SELECT * FROM areas where ida=".$val;
$resultida=mysql_query($queryida);
while($b = mysql_fetch_array($resultida))
{
$nome=$b["nome"];
$idz=$b["ida"];
?> <td style="font-size:9px"> <?php echo "$nome"; ?> </td> <?php
}
}
}
}
///////////////////////////////////////////////////////
$sql="SELECT * FROM relacoes as a inner join areas as b on a.ida=b.ida where b.tipo='AE' or b.tipo='C'";
$resultrelacoes=mysql_query($sql);
$count=mysql_num_rows($resultrelacoes) + 1;
while ($r = mysql_fetch_array($resultrelacoes))
{
$idp=$r["idp"];
$queryidp="SELECT * FROM profissoes where idp=".$idp;
$resultidp=mysql_query($queryidp);
$idacheck=$r["ida"];
if (in_array ($idp , $arrayidp ) )
{
$x=array_search($idacheck, $arrayida);
$res=$x - $xpast;
$a= 5-$xpast;
for ($i=1;$i<=$res;$i++)
{
$pos=$arrayida[$xpast + $i];
/*echo "<script type='text/javascript'>alert('{$pos}');</script>";*/
if($i <> $res )
{
?><td><input type="text" name="<?php echo $pos ?>" id="<?php echo $pos ?>" style="width:40px" /> </td> <?php
}else
{
?><td><input type="text" name="<?php echo $pos ?>" id="<?php echo $pos ?>" value="<?php echo $r["percentagem"]; ?>" style="width:40px" /> </td> <?php
}
if ($i==$a){
?><td> </td> <?php
}
}
}else{
$arrayidp[]=$idp;
while($b = mysql_fetch_array($resultidp))
{
if (array_search($idp,$arrayidp)<>1)
{
$a= 5-$xpast;
$max = sizeof($arrayida);
for ($i=1;$i<=$max - $xpast - 1;$i++)
{
$pos=$arrayida[$xpast + $i];
?> <td><input type="text" name="<?php echo $pos ?>" id="<?php echo $pos ?>" style="width:40px" /> </td> <?php
if($i==$a){
?> <td> </td> <?php
}
if($i==($max-$xpast-1)){
?> <td><input onclick="soma()" type="submit" /></td> <?php
}
}
}
?></form > </tr> <form id="1" ACTION="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi"
METHOD="POST"> <tr> <td style="font-size:9px;"> <?php echo $b['nome']; ?> </td>
<?php
$idacheck=$r["ida"] ;
$x=array_search($idacheck, $arrayida);
for ($i=1;$i<=$x;$i++)
{
$pos=$i;
if($i <> $x)
{
if ($i==5){
?> <td><input type="text" name="<?php echo $pos ?>" id="<?php echo $pos ?>" style="width:40px" /> </td> <td></td><?php
}else{
?> <td><input type="text" name="<?php echo $pos ?>" id="<?php echo $pos ?>" style="width:40px" /> </td> <?php
}
}else
{
?><td><input type="text" name="<?php echo $pos ?>" id="<?php echo $pos ?>" value="<?php echo $r["percentagem"]; ?>" style="width:40px" /> </td> <?php
}
}
}
}
$xpast=$x;
}
$a= 5-$xpast;
$max = sizeof($arrayida);
for ($i=1;$i<=$max - $xpast - 1;$i++){
?> <td><input type="text" name="textfield" id="textfield" style="width:40px" /> </td> <?php
if($i==$a){
?> <td> </td> <?php
}
if($i==($max-$xpast-1)){
?> <td><input type="submit" /></td> <?php
}
}
//FIM DA 1ª TABELA /////////////////////////////////////////////////////// ?>`
我得到了您想要的要点,并且没有使用与您使用的相同的东西,但是如果您根据需要/期望进行调整,这将使您达到目的:
//.sumcolumn - the class of the column you want the sum to be in.
//#tableid - id of table you have all this in
$('#tableid').on('load',function(){
for(var i=0; i<$('tableid').find('tr').length; i++){
var sum = parseInt($('#tableid tr').eq(i).children().eq(0).children('input').val());
sum += parseInt($('#tableid tr').eq(i).children().eq(1).children('input').val());
sum += parseInt($('#tableid tr').eq(i).children().eq(2).children('input').val());
sum += parseInt($('#tableid tr').eq(i).children().eq(3).children('input').val());
sum += parseInt($('#tableid tr').eq(i).children().eq(4).children('input').val());
$('#tableid tr').eq(i).find('.sumcolumn').text(sum);
}
});
这是草率、臃肿、一时冲动的代码,还有更好的方法。但这确实有效。如果你有jQuery
相关文章:
- 限制用户使用按键事件 injQuery 复制和粘贴声明为数字文本框的文本框中的字符
- 使用 javascript 从数字文本输入中获取值并将其乘以单选按钮
- 如果 kendo 数字文本框中的值增加最大值,则希望显示错误消息
- 淘汰剑道数字文本框事件
- 单击语法错误:标识符在数字文本之后立即开始
- 如何判断数字文本框的值是否为正数
- AngularJS数字文本输入:指令与函数
- JSON 数字/文本解析会返回一些奇特的结果
- eval 语法错误:标识符在数字文本之后立即开始
- 将焦点设置为剑道数字文本框
- 语法错误:标识符在数字文本 asp.net 之后立即开始
- kendo ui数字文本框不起作用,步长值为0.001
- 向上或向下点击剑道数字文本框
- 空白或空的数字文本框,而不是零值
- 解析文件显示随机的字母数字文本在textView Android
- 数字文本框和日期选择器在js文件中不工作
- 使用javascript将数字文本替换为字母
- 数字文本框-格式
- 如何用javascript求和多个(未定义的数字)文本框的数量
- SyntaxError:在Firebug中,标识符在数字文本之后立即开始