2010-11-09 288 views
2

我想将一个值传递给一个全局变量。这是shdivs()初始化:javascript全局变量

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

的getElementById( 'currentq')获取div编号。这个想法是,当选择框发生变化时,旧的div被隐藏,显示新的div。但是,警报会更改为我要显示的div(qwe会适当更改),但“旧”var不会更改。所以,当我从列表中选择不同的东西,我得到的东西,如:

警告:SMT警报:AUS再次选择
警告:SMT警报:USD

这意味着变量“老”是未更新。

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

有人能告诉我是什么问题吗?

完整的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> 
+0

你确定你不会再得到另一个错误了你的脚本? – Paddy 2010-11-09 09:27:16

+0

适用于我:http://jsbin.com/akode5。如果getElementById返回null,并且在其他情况下,您会得到一个异常。 – Kobi 2010-11-09 09:29:08

+0

我已经更新了整个代码 – DreamWave 2010-11-09 09:31:26

回答

2

这种情况的最好的解释是,下面的两行中的一个失败。

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.'"... 

无关

小提示你。如果您在Onchange参数传递this,你不必getElementById的选择:

function shdivs(objcurrentq) { 
    qwe=objcurrentq.value; 
+0

你已经ACED了!谢谢! – DreamWave 2010-11-09 09:40:02