2013-08-26 34 views
-1

如果用戶選擇「House」,我想要彈出一個新的選擇框。目前沒有發生。基本javascript無效 - 顯示區域

function showFamilies(){ 

if(pt.selectedIndex == 1) 
document.getElementById('families').style.display = 'block'; 

if(pt.selectedIndex != 1) 
document.getElementById('families').style.display = 'none'; 
} 


<select id="pt" value="pt" name="pt" onChange="showFamilies()"> 
<option value="" name="">Select</option> 
<option value="1" name="1">House</option> 
<option value="2" name="2">Commercial</option> 
</select> 

<div id="families" style="display:none">Families: 
<select name="amin_families"> 
<option value="1" name="Condo">1</option> 
<option value="2" name="Co-Op">2</option></select> 
</div> 
+1

你確定嗎?它在我的瀏覽器中工作。你使用腳本標籤嗎? –

+2

使用'if(expression)語句; else statement2;'而不是'if(expression)語句; if(!expression)statement2;' – BLaZuRE

+0

http://jsbin.com/AsoMaSO你的代碼正在工作。 –

回答

1

重新定義你的if聲明。現在它工作正常。

<!DOCTYPE html> 
<html> 
<head> 
    <script type="text/javascript"> 
    function showFamilies(pt){ 

    if(pt.selectedIndex == 1) 
    { 
    document.getElementById('families').style.display = 'block'; 
    } 
    else 
    { 
    document.getElementById('families').style.display = 'none'; 
    } 
    } 
    </script> 
    <title></title> 
</head> 
<body> 



<select id="pt" value="pt" name="pt" onChange="showFamilies(this)"> 
    <option value="" name="">Select</option> 
    <option value="1" name="1">House</option> 
    <option value="2" name="2">Commercial</option> 
</select> 

<div id="families" style="display:none">Families: 
    <select name="amin_families"> 
     <option value="1" name="Condo">1</option> 
     <option value="2" name="Co-Op">2</option></select> 
</div> 
</body> 
</html> 
+0

重新定義了什麼* loop *? – Phil

+0

沒有工作:(試了一下 – Mike

+2

它沒有工作,你接受的答案嗎?也許你應該告訴我們的全部代碼 –

1

u必須通過this

<select id="pt" value="pt" name="pt" onChange="showFamilies(this)"> 

腳本:

function showFamilies(pt){ 

if(pt.selectedIndex == 1) 
document.getElementById('families').style.display = 'block'; 

if(pt.selectedIndex != 1) 
document.getElementById('families').style.display = 'none'; 
} 
+0

你的情況pt是undefined,我認爲? –

+2

onChange =「showfamilies(this)」hv你看過這個 –