javascript global değişken

0 Cevap php

Ben global bir değişkene bir değer geçmek için çalışıyorum. Shdivs () başlatıldı budur:

<select id="currentq" onchange="shdivs();">

getElementById ('currentq') div kimlikleri alır. Fikir seçin kutusunda bir değişiklik olduğunda, eski div yeni biri gösterilir, gizli olmasıdır. Ancak uyarıları I (uygun qwe değişiklikler) gösterilmesini istediğiniz div değiştirmek, ama 'eski' var değiştirilmez. Ben listeden farklı şeyler seçtiğinizde Yani gibi şeyler olsun:

alert:SMT alert:AUS Select again
alert:SMT alert:USD

Hangi 'eski' değişkeni güncellenmiş değil demektir.

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;
    }

Birisi sorunun ne olduğunu bana söyleyebilir misiniz?

Tüm HTML / PHP / JavaScript kodu:

<?
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>

0 Cevap