Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我想对循环文本框求和_Javascript_Php_Html_While Loop - Fatal编程技术网

Javascript 我想对循环文本框求和

Javascript 我想对循环文本框求和,javascript,php,html,while-loop,Javascript,Php,Html,While Loop,大家好,我是javascript和php新手,我有一个问题 我想对循环文本框求和,但我缺乏逻辑 这是密码 <form name="rec" action="this.php" method="post" > <?php $x = 0; while($x<=5){ $x++; echo "<input type='text' name='n". $x."' id='n". $x."'>< input type='text' name='y". $x

大家好,我是javascript和php新手,我有一个问题 我想对循环文本框求和,但我缺乏逻辑 这是密码

<form name="rec" action="this.php" method="post" > 
<?php 
$x = 0; 
while($x<=5){
$x++; 
echo "<input type='text' name='n". $x."' id='n". $x."'>< input type='text' name='y". $x."' id='y". $x."'>
<input type='text' name='res". $x."' id='res". $x."'>";
}
?>
<input type="button" value="Compute" onclicked="compute()">
</form>

< script > 
ctr = 0<
while(ctr<=5){
ctr++;
x = Number(document.getElementById("n"+ctr).value)
y = Number(document.getElementById("y"+ctr).value)
ans = x+y;
document.getElementById("res"+ctr).value = ans;

} 
< /script>
我想像这样摆脱困境

谢谢我不是很具体:你的计算功能在哪里?您应该使用compute函数将标记的内容括起来。

试试这个

<form name="rec" action="#" method="post">
<?php
$x = 0;
while($x<=5){
$x++;
echo "<input type='text' name='n". $x."' id='n". $x."'><input type='text' name='y". $x."' id='y". $x."'>
<input type='text' name='res". $x."' id='res". $x."'><br>";
}
?>
<input type="button" value="Compute" onclick="compute()">
</form>

<script>
    function compute()
    {

var ctr = 0;
while(ctr<=5){
ctr++;
var x = Number(document.getElementById("n"+ctr).value);
var y = Number(document.getElementById("y"+ctr).value);
var ans = x+y;
document.getElementById("res"+ctr).value = ans;

}
    }
</script>

真的很抱歉,我不知道如何使用stackoverflow T,在脚本