dosgy00204 2012-04-11 00:12
浏览 116
已采纳

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

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. PHP I understand however, and to me this should work. 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).

Now for the most part, the page displays fine, albeit without the javascript changes that are supposed to happen.

I must also apologize for the archaic nature of the code, I inherited this code when I took over as internet guy for our gaming club. 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.

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)

For instance say $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)

<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)+"
CREDITS: "+creds+"
ID: "+ids[i]);
    //  alert("'"+ (cost) + "' > '" + (creds) +"'
"+(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]);     
    }
}

  • 写回答

1条回答 默认 最新

  • douhan5853 2012-04-11 00:23
    关注

    1st issue:

    it has to be getElementById()
    (a lower-case d at the end)


    2nd:

    When using eval, the code will be evaluated as:

    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.

    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:

    document.getElementById('tr'+id).style.background = '#000000';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配