我进行了一些搜索,但没有找到明确的答案。我根本不是一个 javascript 人,而且一无所知。不过我理解 PHP,对我来说这应该可行。我还应该注意,这个脚本过去使用 document.all 作为 javascript,我尝试在可能的情况下将其更新为 getElementById() (因为 document.all 在 firebug 中抛出错误)。
现在,在大多数情况下,页面显示得很好,尽管没有应该发生的 javascript 更改。
我还必须为代码的过时性质道歉,当我接任我们游戏俱乐部的互联网人员时,我继承了这个代码。此代码用于使用虚构积分购买虚构元素。
当我单击一个项目“购买”它(或者可能不购买)时,该行的背景应该变成绿色,并且积分应该从我的总帐户中减去(或者如果我取消选中该框则相反) )。单击“提交”按钮会将我单击的内容添加到另一张工作表中,并从我的帐户中减去实际金额。
当前我收到“tr615 未定义”错误这是 PHP 为该元素生成的代码,如下所示。
如果有人能帮我解决这个问题,那就太棒了。在谷歌和这里搜索了几天后,我似乎找不到答案。
相关代码的 PHP 片段:(我们在网站上使用自定义函数,即:入口)
例如说 $id=615
<?php
while (list ($id, $name, $class, $desc, $range, $damage, $cost,$hide) = entry ($items) )
{
if ($hide =='0')
{
$JavaScriptArrayParms .= '"' . $id . '",';
$list .= $id . ',';
?>
<tr id="tr<?php echo $id; ?>"> //Thus tr615 for this example
<td>
<input type="checkbox" name="chk<?php echo $id; ?>" onclick="updateStoreTable(this.form, this, <?php echo $id; ?>)" />
<input type="hidden" name="cost<?php echo $id; ?>" value="<?php echo $cost; ?>" />
</td>
<td><?php echo $name; ?></td>
<?php if (! in_array($catid, $noclass)){ echo "<td>$class</td>";}?>
<td><?php echo $desc; ?></td>
<?php if (! in_array($catid, $norange)){ echo "<td>$range</td>";}?>
<td><?php echo $damage; ?></td>
<td><?php echo $cost; ?></td>
</tr>
<?php
}
}
?>
</table>
<input type="hidden" name="list" value="<?php echo $list; ?>" />
<input type="button" value="Purchase!" onclick='validatePurchase(this)' />
<input type="reset">
</form>
相关JS:(在某些情况下曾经是document.all.store...或者只是document.all..。我希望我以正确的方式修复了它)
<script language="javascript">
var startmoney = <?php echo $currMoney; ?>;
function canAfford(t,id)
{
if(t.checked) return;// don't touch if checked for buying.
//alert("canAfford("+t+","+id+");");
//t.disabled = false;
eval("document.store.getElementByID(foo).disabled = false;");
eval("document.store.getElementByID(foo).checked = false;");
eval("document.getElementByID(tr"+id+").style.background = '#000000';");
}
function cantAfford(t,id)
{
//alert("cantAfford("+t.disabled+","+id+")-- "+t+";");
//alert("before disable");
//t.disabled = true;
eval("document.store.getElementByID(chk"+id+").disabled = "+true+";");
//alert("After disable");
eval("document.store.getElementByID(chk"+id+").checked = false;");
eval("document.getElementByID(tr"+id+").style.background = '#555555';");
}
function getCost(id)
{
return eval("document.store.getElementByID(cost"+id+").value");
}
function buying(t,id)
{
eval("document.getElementByID(tr"+id+").style.background = 'green';");
document.store.credits.value -= getCost(id);
}
function notbuying(t,id)
{
eval("document.getElementByID(tr"+id+").style.background = '#000000';");
var creds = new Number(document.store.credits.value);
var cost = new Number(getCost(id));
document.store.credits.value = (creds + cost);
}
function updateStoreTable(f,t,id)
{
var ids = new Array(<?php echo $JavaScriptArrayParms; ?>);
if(t.checked)
buying(t,id);
else
notbuying(t,id);
for(i = 0; i<ids.length; i++)
{
cost = new Number(getCost(ids[i]));
creds = new Number(f.credits.value);
//alert("COST: " +(cost)+"\nCREDITS: "+creds+"\nID: "+ids[i]);
// alert("'"+ (cost) + "' > '" + (creds) +"'\n"+(eval(cost > creds)));
// alert("f.chk"+ids[i]+".checked");
if(eval("f.chk"+ids[i]+".checked")) { continue; } //ignore already carted items
if(eval(cost > creds))
cantAfford(eval("f.chk"+id),ids[i]);
else
canAfford(eval("f.chk"+id),ids[i]);
}
}
最佳答案
第一期:
它必须是getElementById()
(末尾有小写d)
第二:
使用 eval 时,代码将被评估为:
document.getElementById(tr615).style.background = '#000000';
..什么会强制错误,因为 tr615 没有用引号引起来,所以 javascript 需要一个变量 tr615。
该行必须如下所示:
eval("document.getElementById('tr"+id+"').style.background = '#000000';");
<小时/>
但是:为什么在这里使用eval,这可以不用eval来完成:
document.getElementById('tr'+id).style.background = '#000000';
关于php - 当 ID 从 PHP 变量传递时,JavaScript 中未定义 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098214/