Wednesday, 7 August 2013

Generating a Table(with an user input) with dropdown list,retrieve selected value from Javascript

Generating a Table(with an user input) with dropdown list,retrieve
selected value from Javascript

I'm generating a table with dropbox in php. I want to check if the user
select number from all dropdown lists, because I need to do calculations
with these values. If generating a table and getting values from dropdown
list easier than generate in php - calculate in javascript please give me
an example.
here is the generating a table part and it is working..
<?php function drawTable($rows, $cols){
echo "<table border='1'>";
$index=0;
echo '<form method="post" action="">';
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($tc=1;$tc<=$cols;$tc++){
echo "<td align='center'>";
if($tr==1 && $tc==1){
echo 'Weights';
}
for($j=2;$j<=$cols;$j++){
if($tr==1 && $tc==$j){
echo $weights[$j-2];
}else if($tr==2 && $tc==$j){
echo $criteria[$j-2];
}
}
for($alt=3;$alt<=$rows;$alt++){
if($tc==1 && $tr==$alt){
echo $criteria[$alt-3];
}
for($box=2;$box<=$cols;$box++){
if($tc==$box && $tr==$alt ){
// echo $alt.$box;
//$hold=($alt-2)*($box-1);
$index++;
echo $index;
?><select name="select" id="<? echo $index ?>"class="change"
onChange="weightCalculate()">
<option value="-" selected="selected">-</option>
<option value="9">8</option>
<option value="4.5">7</option>
<option value="3">6</option>
<option value="2.25">5</option>
<option value="1.8">4</option>
<option value="1.5">3</option>
<option value="1.2857">2</option>
<option value="1.125">1</option>
<option value="1">0</option>
<option value="0.88889">-1</option>
<option value="0.77778">-2</option>
<option value="0.66667">-3</option>
<option value="0.55556">-4</option>
<option value="0.44444">-5</option>
<option value="0.33333">-6</option>
<option value="0.22222">-7</option>
<option value="0.11111">-8</option>
</select> <?php
}
}
}
"</td>";
}
echo "</tr>";
}
echo "</table>";
echo '</form>';
}
drawTable(5,4);
?>
in javascript I want to access each dropdown list and check if all list
selected to calculations.
function weightCalculate(){
for(var id=3;id<=no_of_box;id++){ // i pass the no_of_box to js, this
one is fine
if(document.getElementById('id').value=="-"){ // the problem is
getElementById doesnt work!
return;
}else{
.... for calculations
}
}
How can I check "for each dropdown list" if it is clicked or not, even if
only one dropbox is left not want to go to the calculation part I try
getElementsByTagName but this one also not work correctly. box = new
Array(); box=document.getElementsByTagName("select"); for(var
r=0;r<=box.length;r++){ if(box.item(r).value == "-"){ return; } } Is there
a any other way or best way to doing it maybe dont nees to php,..etc. If
someone can point me in the right direction I would be appreciated.

No comments:

Post a Comment