Php javascript全局变量

Php javascript全局变量,php,javascript,html,Php,Javascript,Html,我试图将一个值传递给一个全局变量。这是shdivs()初始化的地方: <select id="currentq" onchange="shdivs();"> 有人能告诉我问题出在哪里吗 整个HTML/PHP/JavaScript代码: <? echo '<script language="JavaScript"> <!-- var rate = new Array(1,'; $rows = simplexml_load_file('cron/

我试图将一个值传递给一个全局变量。这是shdivs()初始化的地方:

<select id="currentq" onchange="shdivs();">
有人能告诉我问题出在哪里吗

整个HTML/PHP/JavaScript代码:

<?
echo '<script language="JavaScript">
<!--
    var rate = new Array(1,';
    $rows = simplexml_load_file('cron/ecbrates.xml');
    foreach($rows->Cube->Cube->Cube as $rate){
        if ($rate["currency"]=="BGN") echo $rate["rate"].',';
    }
        foreach($rows->Cube->Cube->Cube as $rate){
        if ($rate["currency"]=="USD") echo $rate["rate"].',';
    }
        foreach($rows->Cube->Cube->Cube as $rate){
        if ($rate["currency"]=="GBP") echo $rate["rate"].',';
    }
        foreach($rows->Cube->Cube->Cube as $rate){
        if ($rate["currency"]<>"BGN" && $rate["currency"]<>"USD" && $rate["currency"]<>"GBP")
            if($rate["currency"]=="ZAR") echo $rate["rate"];
            else echo $rate["rate"].',';
    }
    echo ');
    function currency_convert(origin) {
        var origin_value = eval(\'document.currency.c\'+origin+\'.value\');
        var euro_equivalent = rate[origin];
        var v;
        for (i=0; i<rate.length; i++) {
            if (i!=origin) {
                v = Math.round(rate[i]*origin_value/euro_equivalent*10000)/10000;
                eval(\'document.currency.c\'+i+\'.value = \'+v);
            }
        }
        return true;
    }
// -->
</script>';
?>
<div style="position:relative;">
<form name="currency">
<table cellspacing="0" cellpadding="0" border="1" bordercolor="#336699">
<tr><tr>
<td><img src="flags/eur.jpg" alt="Europe" width="30" height="20"></td>
<td>EUR</td>
<td><input type="text" name="c0" value="" size="10" onKeyUp="currency_convert(0);"></td></tr><tr>
<td><img src="flags/bgn.gif" alt="USA" width="30" height="20"></td>
<td>BGN</td>
<td><input type="text" name="c1" value="" size="10" onKeyUp="currency_convert(1);"></td></tr><tr>
<td><img src="flags/usd.gif" alt="Austria" width="30" height="20"></td>
<td>USD</td>
<td><input type="text" name="c2" value="" size="10" onKeyUp="currency_convert(2);"></td></tr><tr>
<td><img src="flags/gbp.gif" alt="Belgium" width="30" height="20"></td>
<td>GBP</td>
<td><input type="text" name="c3" value="" size="10" onKeyUp="currency_convert(3);"></td></tr><tr>
<td colspan="2">
<select id="currentq" onchange="shdivs();">
<?
foreach($rows->Cube->Cube->Cube as $rate){
    if ($rate["currency"]<>"BGN" && $rate["currency"]<>"USD" && $rate["currency"]<>"GBP") {
        echo '<option value="'.$rate['currency'].'">'.$rate['currency'].'</option>';
    }
}
?>
</select>
</td>
<td>
<?
$numb=4;
foreach($rows->Cube->Cube->Cube as $rate){
    if ($rate["currency"]<>"BGN" && $rate["currency"]<>"USD" && $rate["currency"]<>"GBP") {
        echo '<div style="position:absolute; top:99px; visibility:hidden;" id="'.$rate["currency"].'"><input type="text" id="'.$rate["currency"].'" name="c'.$numb.'" value="" size="10" onKeyUp="currency_convert(4);"></div>';
        $numb++;
    }
}
?>

    </td>
</tr></table>
</form>
</div>
<script type="text/javascript">
    old="SMT";
    function shdivs() {
        alert(old);
        qwe=document.getElementById('currentq').value;
        alert(qwe);
        document.getElementById(qwe).style.visibility="visible";
        document.getElementById(old).style.visibility="hidden";
        old=qwe;
    }
    </script>
Cube->Cube->Cube作为$rate){
如果($rate[“currency”]=“BGN”)回显$rate[“rate”];
}
foreach($rows->Cube->Cube->Cube as$费率){
如果($rate[“currency”]=“USD”)与$rate[“rate”]。,';
}
foreach($rows->Cube->Cube->Cube as$费率){
如果($rate[“currency”]=“GBP”)回显$rate[“rate”];
}
foreach($rows->Cube->Cube->Cube as$费率){
如果($rate[“currency”]“BGN”&&$rate[“currency”]“USD”&&$rate[“currency”]“GBP”)
如果($rate[“currency”]=“ZAR”)响应$rate[“rate”];
else echo$rate[“rate”];
}
回声';
功能货币转换(原点){
var origin\u value=eval(\'document.currency.c\'+origin+\'.value\');
var欧元=汇率[来源];
var v;
对于(i=0;i
';
?>
欧元
BGN
美元
英镑
old=“SMT”;
函数shdivs(){
警惕(旧);
qwe=document.getElementById('currentq').value;
警报(qwe);
document.getElementById(qwe.style.visibility=“visible”;
document.getElementById(旧).style.visibility=“hidden”;
old=qwe;
}

对此最好的解释是以下两行中的一行出现故障

    document.getElementById(qwe).style.visibility="visible";
    document.getElementById(old).style.visibility="hidden";
您能否验证select引用的所有ID是否都存在于DOM中

将警报移动到函数的末尾,然后查看再次运行时是否会弹出警报

编辑

由于您发布了整个代码,我可以看到一个潜在的问题

在编写DIV的代码中,您已将相同的id分配给两个元素。id应是唯一的:

 ...id="'.$rate["currency"].'"><input type="text" id="'.$rate["currency"].'" name="c'.$numb.'"...

你确定你的脚本没有进一步出现另一个错误吗?对我来说很有用:。如果getElementById返回null,以及在其他情况下,你会得到一个异常。我已经用整个代码更新了。在这个实例中,呈现的代码会更有帮助。你能在将代码加载到浏览器中后发布代码吗。脱离主题:几乎总是有比
eval
更好的方法;在本例中,它是
var origin\u value=document.currency['c'+origin+].value;
。此外,您还可以使用匿名函数将
old
转换为局部变量,立即求值:
(function(){var old=“SMT”;shdivs=function(objcurrentq){var qwe;…})(
)。
 ...id="'.$rate["currency"].'"><input type="text" id="'.$rate["currency"].'" name="c'.$numb.'"...
function shdivs(objcurrentq) {
    qwe=objcurrentq.value;