简体   繁体   English

当 ID 从 PHP 变量传递时,javascript 中的未定义 ID

[英]undefined ID in javascript when ID is passed from PHP variable

I've done some searches and I've come up with no clear answer.我做了一些搜索,但没有找到明确的答案。 I'm not a javascript person at all and am pretty clueless.我根本不是 javascript 的人,而且我很无能。 PHP I understand however, and to me this should work. PHP 不过我明白了,对我来说这应该可行。 I should also note, that this script used to use document.all for it's javascript, which I've tried to update to getElementById() when possible (since document.all was throwing an error in firebug).我还应该注意,这个脚本曾经使用 document.all,因为它是 javascript,我尽可能地尝试将其更新为 getElementById()(因为 document.all 在萤火虫中抛出错误)。

Now for the most part, the page displays fine, albeit without the javascript changes that are supposed to happen.现在大部分情况下,页面显示正常,尽管没有应该发生的 javascript 更改。

I must also apologize for the archaic nature of the code, I inherited this code when I took over as inte.net guy for our gaming club.我还必须为代码的陈旧性质道歉,当我接任我们游戏俱乐部的 inte.net 人时,我继承了这段代码。 This code is for the purchase of fictional items using fictional credits.此代码用于使用虚构信用购买虚构物品。

When I click an item to "buy" it (or maybe not whichever) The background of that row is supposed to turn green, and the credits are supposed to be subtracted from my total account (or reverse if I uncheck the box).当我点击一个项目来“购买”它(或者可能不是任何一个)时,该行的背景应该变成绿色,并且信用应该从我的总账户中减去(或者如果我取消选中该框则相反)。 Clicking the submit button adds this stuff I clicked to another sheet, and subtracts the actual amount from my account.单击提交按钮会将我单击的这些内容添加到另一张表中,并从我的帐户中减去实际金额。

Currently I get a "tr615 is undefined" error This is the PHP generated code for the element as shown below.目前我收到“tr615 未定义”错误这是 PHP 为元素生成的代码,如下所示。

If someone can help me figure this out it would fantastic.如果有人能帮我解决这个问题,那就太棒了。 I just can't seem to find an answer after a few days of searching google and here.在谷歌和此处搜索几天后,我似乎找不到答案。

PHP Snippet of relevent code: (we use custom functions on our site ie: entry) PHP 相关代码片段:(我们在网站上使用自定义功能,即:条目)

For instance say $id=615例如说 $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>

Relevant JS: (which used to be document.all.store... or just document.all.. in some cases. I hope I fixed it the right way)相关 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]);     
    }
}

1st issue:第一期:

it has to be getElementById()它必须是getElementById()
(a lower-case d at the end) (最后一个小写的d


2nd:第二:

When using eval, the code will be evaluated as:使用 eval 时,代码将被评估为:

document.getElementById(tr615).style.background = '#000000';

..what will force the error, because the tr615 is not enclosed by quotes, so javascript expects a variable tr615. ..什么会导致错误,因为 tr615 没有用引号括起来,所以 javascript 需要一个变量 tr615。

the line must look like this:该行必须如下所示:

eval("document.getElementById('tr"+id+"').style.background = '#000000';");

But: Why do you use eval here, this can be done without eval:但是:这里为什么要用eval,这个不用eval也可以做到:

document.getElementById('tr'+id).style.background = '#000000';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM