<FORM name='form1'><TABLE>
server scripting loop (php), generates X (a variable number) lines
with the 3 fields {
<TR><TD><input type='text' name='kminicial[]'></TD>
<TD><input type='text' name='kmfinal[]'
onBlur='document.form1.kmrodados[currentindex].value=document.form1.kmfinal[currentindex].value-
document.form1.kminicial[currentindex].value'></TD>
<TD><input type='text' name='kmrodados[]'></TD></TR>
}
</TABLE></FORM>
Just a simple onBlur math with the first two fields of each line,
putting the result on the third. The problem is the "currentindex"
value, I wasnt able to find how to get this value anywhere, is it
possible to implement this way?
Thanks in advance,
Norman
If you use PHP, why don't you do an extra step?
<?php
$rowid = 0;
?>
...
<input type=" name="kmfinal[]"
onblur="updateResultByRowId(<? echo $rowid++; ?>)">
Otherwise, I can only think of this:
<input type=" name="kmfinal[]"
onblur="updateResultByRightField(this)">
function updateResultByRightField(rightField){
var currentForm = rightField.form;
/*
* And then write code to find:
* var leftField = ......
* var resultField
*
* ... either going up from rightField or going
* down from currentForm...
*/
}
One more idea is to asign events to the row but I still prefer option 1 :)
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Thanks Alvaro, I tried your 1st approach:
<SCRIPT LANGUAGE=\"JavaScript\">
function calcula(indice) {
document.formulario.kmrodados[indice].value =
document.formulario.kmfinal[indice]-
document.formulario.kminicial[indice];
return true;
}
</SCRIPT>
and later, initialize the $indiceatual=0 on php, and inside the loop:
<TD><input type='text' name='kminicial[]'>
<TD><input type='text' name='kmfinal[]' onBlur='calcula(" .
$indiceatual++ . ");'>
<TD><input type='text' name='kmrodados[]'>
but this generates an javascript error stating:
this.document.formulario.kmrodados has no properties
on the 1st line of the calcula() function. The form name is declared
as 'formulario'. Can you see what Im doing wrong here?
Thank in advance,
Norman
document.formulario.elements['kmrodados[]']
[indice].value=document.formulario.elements['kmfinal[]'][indice].value-
document.formulario.elements['kminicial[]'][indice].value;
Norman
Should be
var es = document.forms['formulario'].elements;
...
es['kmrodados[]'][indice].value =
es['kmfinal[]'][indice].value - es['kminicial[]'][indice].value;
instead. Probably you don't need document.forms['formulario'] and the
associated form name anyway, because you can pass the form object reference
from an included form control with `this.form', and from the `form' element
with `this'.
Also note that the `-' operation performs only a simple conversion (from
string) to number, which may lead to unexpected results e.g. with "0x42",
"077", or "078". You may want to use parseInt(..., 10) or parseFloat(...)
instead.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>