2016-01-29 33 views
0

知道一点Javascript是希望有人能帮助我。根据DDL中的值,我试图让DIV显示。Javascript显示DIV的DDL值

这是我到目前为止有:

  <asp:DropDownList ID="SearchTypeDDL" runat="server" onchange="searchType();" Width="150px"> 
       <asp:ListItem Enabled="true" Text="Select Search type" Value="-1"></asp:ListItem> 
       <asp:ListItem Text="Coordinates" Value="1"></asp:ListItem> 
       <asp:ListItem Text="Postcode/Town" Value="2"></asp:ListItem>    
      </asp:DropDownList> 

的Javascript:

function searchType() { 
     var ddl = document.getElementById("<%=SearchTypeDDL.ClientID %>"); 
     if (ddl.selectedIndex == '1') { 
      $('#coordinates').show(); 
     } else { 
      $('#coordinates').hide(); 
     } 
    } 

编辑:

新增警报检查值传递:

var ddl = document.getElementById("ddl"); 
     var SelectedIndex = ddl.selectedIndex; 
     alert(SelectedIndex); 
     if (ddl.selectedIndex == '1') { 
      $('#coordinates').show();    
     } else { 
      $('#coordinates').hide(); 
     } 

警报是sho翼'1',但它跳过了IF声明

回答

0

如果其他人有类似的问题,这里是我用来获取它的工作

function searchType() { 
     var ddl = document.getElementById("ddl"); 
     var SelectedIndex = ddl.selectedIndex;   
     if (SelectedIndex == '1') { 
      document.getElementById('coordinates').style.display = "block";        
     } else (SelectedIndex== '1') { 
      document.getElementById('coordinates').style.display = "none"; 
     } 
    } 
0

什么不起作用?

我复制你的代码为JSFiddle,它似乎只是正常工作

<select id="ddl" onChange="searchType()"> 
    <option value="-1">Select Search Type</option> 
    <option value="1">Coordinates</option> 
    <option value="2">Postcode</option> 
</select> 

<div id="coordinates" style="display:none"> 
    Coordinates div 
</div> 

window.searchType = function() { 
    var ddl = document.getElementById("ddl"); 
    if (ddl.selectedIndex == '1') { 
    $('#coordinates').show(); 
    } else { 
    $('#coordinates').hide(); 
    } 
}