Array in For lus
stenno - 05/10/2005 16:09 (laatste wijziging 05/10/2005 16:12)
PHP interesse
Ik heb een tabel waarin ik 3 kolommen heb. Na een klik op de search button moet in de eerste kolom het antwoord komen van waarde kolom 2 maal waarde kolom 3. Ik heb alle names van de input's uit 2 letters gescript.
In kolom 1 beginnen alle names met a. dus kolom 1 rij 1 is name="aa".
kolom 1 rij 2 is name="ab" kolom 2 rij 4 is dus name="bd".
Nu kan ik er met een for lus voor kiezen om 3 apparte Array's te maken. Oftewel voor elke kolom een aparte array !
NU is mijn vraag kan ik niet een Array maken met alleen de laatste letter van elke name. Dus zo: var xx = Array('a','b','c') // enzovoorts.
en dan de verwijzing een beetje aanpassen.
ik zal ff een voorbeeld script posten:
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var aa= Array('a','b','c','d','e','f','g','h','i','j')
var y= aa.length
function a()
{
for(x=0;x<y;x++)
{
document.form1.elements[aa[x]].value= parseFloat(document.form1.elements[aa[x]].value)*parseFloat(document.form1.elements[aa[x]].value)
}
}
//-->
</script>
</head>
<body>
<form name="form1">
<table>
<tr>
<td><input type="text" name="aa" value="" /></td>
<td><input type="text" name="ba" value="1"/></td>
<td><input type="text" name="ca" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ab" value=""/></td>
<td><input type="text" name="bb" value="2"/></td>
<td><input type="text" name="cb" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ac" value=""/></td>
<td><input type="text" name="bc" value="3"/></td>
<td><input type="text" name="cc" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ad" value=""/></td>
<td><input type="text" name="bd" value="4"/></td>
<td><input type="text" name="cd" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ae" value=""/></td>
<td><input type="text" name="be" value="5"/></td>
<td><input type="text" name="ce" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="af" value=""/></td>
<td><input type="text" name="bf" value="6"/></td>
<td><input type="text" name="cf" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ag" value=""/></td>
<td><input type="text" name="bg" value="7"/></td>
<td><input type="text" name="cg" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ah" value=""/></td>
<td><input type="text" name="bh" value="8"/></td>
<td><input type="text" name="ch" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="ai" value=""/></td>
<td><input type="text" name="bi" value="9"/></td>
<td><input type="text" name="ci" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aj" value=""/></td>
<td><input type="text" name="bj" value="10"/></td>
<td><input type="text" name="cj" value="1"/></td>
</tr>
</table>
<input type="button" value="search" onClick="a()" />
</form>
</body>
</html>
< html>
< head>
< title> Untitled</ title>
<script language = "JavaScript" type= "text/javascript" >
<!--
var aa
= Array ( 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ) var y= aa. length
function a( )
{
for ( x= 0 ; x< y; x++ )
{
document. form1. elements[ aa[ x] ] . value= parseFloat( document. form1. elements[ aa[ x] ] . value) * parseFloat( document. form1. elements[ aa[ x] ] . value)
}
}
//-->
</script>
</ head>
< body>
< form name= "form1" >
< table>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "ba" value= "1" /></ td>
< td>< input type= "text" name= "ca" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ab" value= "" /></ td>
< td>< input type= "text" name= "bb" value= "2" /></ td>
< td>< input type= "text" name= "cb" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ac" value= "" /></ td>
< td>< input type= "text" name= "bc" value= "3" /></ td>
< td>< input type= "text" name= "cc" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ad" value= "" /></ td>
< td>< input type= "text" name= "bd" value= "4" /></ td>
< td>< input type= "text" name= "cd" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ae" value= "" /></ td>
< td>< input type= "text" name= "be" value= "5" /></ td>
< td>< input type= "text" name= "ce" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "af" value= "" /></ td>
< td>< input type= "text" name= "bf" value= "6" /></ td>
< td>< input type= "text" name= "cf" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ag" value= "" /></ td>
< td>< input type= "text" name= "bg" value= "7" /></ td>
< td>< input type= "text" name= "cg" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ah" value= "" /></ td>
< td>< input type= "text" name= "bh" value= "8" /></ td>
< td>< input type= "text" name= "ch" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "ai" value= "" /></ td>
< td>< input type= "text" name= "bi" value= "9" /></ td>
< td>< input type= "text" name= "ci" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aj" value= "" /></ td>
< td>< input type= "text" name= "bj" value= "10" /></ td>
< td>< input type= "text" name= "cj" value= "1" /></ td>
</ tr>
</ table>
< input type= "button" value= "search" onClick= "a()" />
</ form>
</ body>
</ html>
in dit script gaat het dus om het volgende regeltje
document.form1.elements[aa[x]].value= parseFloat(document.form1.elements[aa[x]].value)*parseFloat(document.form1.elements[aa[x]].value)
document. form1. elements[ aa[ x] ] . value= parseFloat( document. form1. elements[ aa[ x] ] . value) * parseFloat( document. form1. elements[ aa[ x] ] . value)
hoe pas ik dit regeltje zo aan dat er dus uit komt in de eerste kolom het antwoord van kolom 2 maal kolom 3 ???
Graag help !!! (haytjes je zal dit vast wel weten, HOOP IK ^^ )
8 antwoorden
Gesponsorde links
haytjes - 05/10/2005 16:22 (laatste wijziging 05/10/2005 16:29)
JS gevorderde
'k ga ne keer kijken:)
[edit]
bedoel je zo?:
document.form1.elements["a"+aa[x]].value= parseFloat(document.form1.elements["b"+aa[x]].value)*parseFloat(document.form1.elements["c"+aa[x]].value)
document. form1. elements[ "a" + aa[ x] ] . value= parseFloat( document. form1. elements[ "b" + aa[ x] ] . value) * parseFloat( document. form1. elements[ "c" + aa[ x] ] . value)
haytjes - 05/10/2005 16:39 (laatste wijziging 05/10/2005 16:40)
JS gevorderde
'k denk da dit nog beter is.
want je hebt nu 3* <input name='a'>
'k kan ook mis zijn
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var aa= Array('a','b','c','d','e','f','g','h','i','j')
var y= aa.length
function tel()
{
for(x=0;x<y;x++)
{
oInput = document.form1.elements[aa[x]];
oInput[0].value = parseFloat(oInput[1].value)*parseFloat(oInput[2].value);
}
}
//-->
</script>
</head>
<body>
<form name="form1">
<table>
<tr>
<td><input type="text" name="a" value="" /></td>
<td><input type="text" name="a" value="1"/></td>
<td><input type="text" name="a" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="b" value=""/></td>
<td><input type="text" name="b" value="2"/></td>
<td><input type="text" name="b" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="c" value=""/></td>
<td><input type="text" name="c" value="3"/></td>
<td><input type="text" name="c" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="d" value=""/></td>
<td><input type="text" name="d" value="4"/></td>
<td><input type="text" name="d" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="e" value=""/></td>
<td><input type="text" name="e" value="5"/></td>
<td><input type="text" name="e" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="f" value=""/></td>
<td><input type="text" name="f" value="6"/></td>
<td><input type="text" name="f" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="g" value=""/></td>
<td><input type="text" name="g" value="7"/></td>
<td><input type="text" name="g" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="h" value=""/></td>
<td><input type="text" name="h" value="8"/></td>
<td><input type="text" name="h" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="i" value=""/></td>
<td><input type="text" name="i" value="9"/></td>
<td><input type="text" name="i" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="j" value=""/></td>
<td><input type="text" name="j" value="10"/></td>
<td><input type="text" name="j" value="1"/></td>
</tr>
</table>
<input type="button" value="search" onClick="tel()" />
</form>
</body>
</html>
< html>
< head>
< title> Untitled</ title>
<script language = "JavaScript" type= "text/javascript" >
<!--
var aa
= Array ( 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ) var y= aa. length
function tel( )
{
for ( x= 0 ; x< y; x++ )
{
oInput = document. form1. elements[ aa[ x] ] ;
oInput[ 0 ] . value = parseFloat( oInput[ 1 ] . value) * parseFloat( oInput[ 2 ] . value) ;
}
}
//-->
</script>
</ head>
< body>
< form name= "form1" >
< table>
< tr>
< td>< input type= "text" name= "a" value= "" /></ td>
< td>< input type= "text" name= "a" value= "1" /></ td>
< td>< input type= "text" name= "a" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "b" value= "" /></ td>
< td>< input type= "text" name= "b" value= "2" /></ td>
< td>< input type= "text" name= "b" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "c" value= "" /></ td>
< td>< input type= "text" name= "c" value= "3" /></ td>
< td>< input type= "text" name= "c" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "d" value= "" /></ td>
< td>< input type= "text" name= "d" value= "4" /></ td>
< td>< input type= "text" name= "d" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "e" value= "" /></ td>
< td>< input type= "text" name= "e" value= "5" /></ td>
< td>< input type= "text" name= "e" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "f" value= "" /></ td>
< td>< input type= "text" name= "f" value= "6" /></ td>
< td>< input type= "text" name= "f" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "g" value= "" /></ td>
< td>< input type= "text" name= "g" value= "7" /></ td>
< td>< input type= "text" name= "g" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "h" value= "" /></ td>
< td>< input type= "text" name= "h" value= "8" /></ td>
< td>< input type= "text" name= "h" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "i" value= "" /></ td>
< td>< input type= "text" name= "i" value= "9" /></ td>
< td>< input type= "text" name= "i" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "j" value= "" /></ td>
< td>< input type= "text" name= "j" value= "10" /></ td>
< td>< input type= "text" name= "j" value= "1" /></ td>
</ tr>
</ table>
< input type= "button" value= "search" onClick= "tel()" />
</ form>
</ body>
</ html>
stenno - 05/10/2005 18:20
PHP interesse
Ja inderdaad klopt,
bij nader inzien heb je trouwens helemaal geen Array() nodig.
Dit kan volgens mij namelijk ook:
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function tel()
{
for(x=0;x<30;x++)
{
document.form1.aa[x].value= parseFloat(document.form1.aa[x+1].value)*parseFloat(document.form1.aa[x+2].value);
}
}
//-->
</script>
</head>
<body>
<form name="form1">
<table>
<tr>
<td><input type="text" name="aa" value="" /></td>
<td><input type="text" name="aa" value="1"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="2"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="3"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="4"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="5"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="6"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="7"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="8"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="9"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
<tr>
<td><input type="text" name="aa" value=""/></td>
<td><input type="text" name="aa" value="10"/></td>
<td><input type="text" name="aa" value="1"/></td>
</tr>
</table>
<input type="button" value="search" onClick="tel()" />
</form>
</body>
</html>
< html>
< head>
< title> Untitled</ title>
<script language = "JavaScript" type= "text/javascript" >
<!--
function tel( )
{
for ( x= 0 ; x< 30 ; x++ )
{
document. form1. aa[ x] . value= parseFloat( document. form1. aa[ x+ 1 ] . value) * parseFloat( document. form1. aa[ x+ 2 ] . value) ;
}
}
//-->
</script>
</ head>
< body>
< form name= "form1" >
< table>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "2" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "3" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "4" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "5" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "6" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "7" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "8" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "9" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
< tr>
< td>< input type= "text" name= "aa" value= "" /></ td>
< td>< input type= "text" name= "aa" value= "10" /></ td>
< td>< input type= "text" name= "aa" value= "1" /></ td>
</ tr>
</ table>
< input type= "button" value= "search" onClick= "tel()" />
</ form>
</ body>
</ html>
stenno - 05/10/2005 18:52 (laatste wijziging 05/11/2005 11:40)
PHP interesse
Nog een vraagje weet iemand hoe ik dat hier mee doe ??
<?php
mysql_connect('192.168.0.11','u56452099','loet06') or die("couldn't connect to the database. Please try again.");
mysql_select_db('u56452099') or die(mysql_error());
?>
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function NumberFormat(num, inputDecimal)
{
function a()
{
var y = document.form1.test.length;
for (x=0; x<y; x++)
{
// HET GAAT DUS OM DIT ONDERSTAANDE STUK !!!
var obj = document.getElementById('test');
var num = new NumberFormat();
num.setNumber(obj.innerHTML);
num.setPlaces('0');
num.setSeparators(true, ',', ',');
obj.innerHTML = num.toFormatted();
}
}
//-->
</script>
</head>
<body onload="a()">
<form name="form1">
<center>
<table width="650" border="1" bordercolor="" style="border-collapse:collapse;" cellpadding="3" cellspacing="0">
<tr bgcolor="darkblue">
<th>#</th>
<th>Nickname</th>
<th>Rank</th>
<th>Sentry rating</th>
<th>Spy rating</th>
<th>Reason</th>
<?php
$res = mysql_query("SELECT id,nickname,rank,sentry,spy,reason,spylink FROM targets");
while($obj=mysql_fetch_assoc($res))
{
?>
<tr>
<td><?=$obj['id']?></td>
<td><a class="type1"href="<?=$obj['spylink'];?>"><?=$obj['nickname'];?></a></td>
<td id="test" name="test"><?=$obj['rank']?></td>
<td id="test" name="test"><?=$obj['sentry']?></td>
<td id="test" name="test"><?=$obj['spy']?></td>
<td><?=$obj['reason']?></td>
</tr>
<?php
}
?>
</table>
</center>
</form>
</body>
</html>
<?php
mysql_connect ( '192.168.0.11' , 'u56452099' , 'loet06' ) or
die ( "couldn't connect to the database. Please try again." ) ; ?>
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function NumberFormat(num, inputDecimal)
{
function a()
{
var y = document.form1.test.length;
for (x=0; x<y; x++)
{
// HET GAAT DUS OM DIT ONDERSTAANDE STUK !!!
var obj = document.getElementById('test');
var num = new NumberFormat();
num.setNumber(obj.innerHTML);
num.setPlaces('0');
num.setSeparators(true, ',', ',');
obj.innerHTML = num.toFormatted();
}
}
//-->
</script>
</head>
<body onload="a()">
<form name="form1">
<center>
<table width="650" border="1" bordercolor="" style="border-collapse:collapse;" cellpadding="3" cellspacing="0">
<tr bgcolor="darkblue">
<th>#</th>
<th>Nickname</th>
<th>Rank</th>
<th>Sentry rating</th>
<th>Spy rating</th>
<th>Reason</th>
<?php
$res = mysql_query ( "SELECT id,nickname,rank,sentry,spy,reason,spylink FROM targets" ) ; {
?>
<tr>
<td><?= $obj [ 'id' ] ?> </td>
<td><a class="type1"href="<?= $obj [ 'spylink' ] ; ?> "><?= $obj [ 'nickname' ] ; ?> </a></td>
<td id="test" name="test"><?= $obj [ 'rank' ] ?> </td>
<td id="test" name="test"><?= $obj [ 'sentry' ] ?> </td>
<td id="test" name="test"><?= $obj [ 'spy' ] ?> </td>
<td><?= $obj [ 'reason' ] ?> </td>
</tr>
<?php
}
?>
</table>
</center>
</form>
</body>
</html>
Hoe ga ik dit stuk goed verwerken in een forlus ??
var obj = document.getElementById('test');
var obj = document. getElementById( 'test' ) ;
ER moet dus op de een of andere manier een +[x] bij of zo
haytjes - 05/11/2005 12:39 (laatste wijziging 05/11/2005 12:43)
JS gevorderde
via hetvolgende krijg je een array:
var obj = document.getElementsByName('test');
var obj = document. getElementsByName( 'test' ) ;
:!: hier gebruik ik NAME
obj.length = hoeveel;
obj[0-....] = De 1ste -> ...de
for(x=0;x<obj.length;x++)
{
obj[x].kweetniewat = ....;
}
obj. length = hoeveel;
obj[ 0 -.... ] = De 1ste -> ... de
for ( x= 0 ; x< obj. length; x++ )
{
obj[ x] . kweetniewat = ....;
}
Er zit wel nog een foutje in je script:
Citaat:
function NumberFormat(num, inputDecimal)
{
...
function a()
{
...
}
}
dit moet hetvolgende zijn:
function NumberFormat(num, inputDecimal)
{
...
}
function a()
{
...
}
function NumberFormat( num, inputDecimal)
{
...
}
function a( )
{
...
}
Gesponsorde links
Dit onderwerp is gesloten .