2017-09-01 3 views
0

私はデータを収集するためのWebベースのツールで作業しています。入力フォームには、表のセルに配置されたテキスト入力が含まれています。フォームに数字が入力されると、セルの背景色が変わります。フォームの入力に基づいてセルの背景を変更する(PHPおよびJS)

入力が1つだけの場合、私が現在まとめているコードはうまく機能します。フォームに入力を追加すると、最後に変更されたセルだけが表示されます。私はそれがjavascript関数内のセル参照と関係があると思われる。

<?php 
$LSL = 2; 
$LCL = 4; 
$NOM = 6; 
$UCL = 8; 
$USL = 10; 


function makeCell($InputID, $LSL, $LCL, $NOM, $UCL, $USL) { 
    $Cell = "C".$InputID; 

    print "<td id=\"$Cell\">\n"; 
    print "<input id=\"$InputID\" name=\"".$InputID."23\" onchange=\"jsonchange('$Cell')\"/>\n"; 
    print "<script>\n"; 

    print "function jsonchange(id) {\n"; 
    print "if (document.getElementById(\"$InputID\").value <= $LCL) { document.getElementById(\"$Cell\").style.backgroundColor = \"yellow\"; }\n"; 
    print "if (document.getElementById(\"$InputID\").value <= $LSL) { document.getElementById(\"$Cell\").style.backgroundColor = \"red\"; }\n"; 
    print "if (document.getElementById(\"$InputID\").value >= $UCL) { document.getElementById(\"$Cell\").style.backgroundColor = \"yellow\"; }\n"; 
    print "if (document.getElementById(\"$InputID\").value >= $USL) { document.getElementById(\"$Cell\").style.backgroundColor = \"red\"; }\n"; 
    print "if (document.getElementById(\"$InputID\").value > $LCL && document.getElementById(\"$InputID\").value < $UCL) { document.getElementById(\"$Cell\").style.backgroundColor = \"white\"; }\n"; 
    print "}\n"; 
    print "</script>\n"; 
    print "</td>\n"; 
} 


print "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width: 99%;\">\n"; 
print "<tr>\n"; 

makeCell(1, $LSL, $LCL, $NOM, $UCL, $USL); 
makeCell(2, $LSL, $LCL, $NOM, $UCL, $USL); 
makeCell(3, $LSL, $LCL, $NOM, $UCL, $USL); 
makeCell(4, $LSL, $LCL, $NOM, $UCL, $USL); 

print "</tr>\n"; 
print "</table>\n"; 
?> 

答えて

0

あなたはリスナーすべてへの入力を割り当てたいことがあります。

window.addEventListener("input",function(e){ 
var val = e.target.value; 
if(val < 15){ 
    e.target.style.color = "red"; 
}else{ 
    e.target.style.color = "blue"; 
}); 

あなたの現在のコードdoesntの仕事原因はすべての機能が名前の名前を持っています。彼らがお互いを上書きすることを意味する。

+0

関数名はまさにそれでした。ありがとう – SBrewer

関連する問題