2017-05-12 179 views
0

我有選擇下拉列表給它一個空的問題,而選擇了它後選擇下拉列表選項顯示隱藏的文本框

下面是代碼:

<asp:GridView ID="gridview1" runat="server" AllowPaging="false" Width="99%" > 

    <asp:BoundField DataField="NAME" HeaderText ="Name" HtmlEncode="false"/> 

    <asp:BoundField DataField="ID " HeaderText ="ID " HtmlEncode="false"/> 

     <asp:TemplateField HeaderText ="Location"> 
     <ItemTemplate> 

    <asp:DropDownList ID="ddlfruitAgent" runat="server" onchange="Store_Location_onChange(this)" /> 
    <asp:TextBox ID="StoreLocation" runat="server" TextMode="MultiLine" Rows="2" style="width:97%;display:none" /> 
     </ItemTemplate> 
     </asp:TemplateField> 

的javascript:

function Store_Location_onChange(objThis) { 
    if (objThis.value == "0") { 

     document.getElementById("StoreLocation").style.display = ""; 
    } 
    else { 
     document.getElementById("StoreLocation").value = ""; 
     document.getElementById("StoreLocation").style.display = "none"; 
    } 
} 

回答

1

由於你傳遞this爲對象的參考,你可以這樣做

function Store_Location_onChange(objThis) { 
    if (objThis.value == "0") { 
     objThis.nextElementSibling.style.display = ""; 
    } 
    else { 
     objThis.nextElementSibling.value = ""; 
     objThis.nextElementSibling.style.display = "none"; 
    } 
} 

或其他方式周圍是

function Store_Location_onChange(objThis) { 
    if (objThis.value == "0") { 
     document.getElementById("<%= StoreLocation.ClientID %>").style.display = ""; 
    } 
    else { 
     documentgetElementById("<%= StoreLocation.ClientID %>").value = ""; 
     document.getElementById("<%= StoreLocation.ClientID %>").style.display = "none"; 
    } 
} 

希望這有助於!

+0

它有一個錯誤的JavaScript運行時錯誤:無法設置定義或空引用屬性值 而我嘗試選擇下拉列表 –

+0

控制檯這一行' - > objThis.nextElementSibling'並查看返回的是什麼! –

+0

下拉列表是後面的代碼是一個SQL查詢 –

相關問題