根据用户从下拉列表中的选择显示/隐藏文本框或整个 div 部分的最佳方式是什么?我不相信它可能与服务器控件一起使用,所以我必须使用常规的客户端 HTML 控件,对吗?感谢您的任何输入。 jQuery 会是最好的选择吗?
基于下拉选择,我希望能够显示下面的Div,并且默认隐藏该Div。想法?:
<div id="divLimitPrice">Limit Price<br />
<asp:TextBox ID="txtLimitPrice" runat="server" ValidationGroup="ValidationGroupOrder"> </asp:TextBox>
最佳答案
您可以像使用简单的 html 控件一样使用服务器控件来实现。您只需要正确设置控件呈现的客户端 ID。这是一个示例:(有关我的操作,请参阅代码注释)
function TriggerChange(me)
{
// get the drop down control
var cTheDropDown = jQuery("#<%=ddlControl.ClientID%>");
// find the value of the selected one
var SelValue = jQuery('option:selected', cTheDropDown).attr('value');
// now do what you like with it
if(SelValue == "open")
jQuery("#divLimitPrice").show();
else
jQuery("#divLimitPrice").hide();
}
一个较短的版本
function TriggerChange(me)
{
// get the selected value from the drop down list
// and base on it, show or hide your div
if(jQuery("#<%=ddlControl.ClientID%>").val() == "open")
jQuery("#divLimitPrice").show();
else
jQuery("#divLimitPrice").hide();
}
然后在控件上添加触发器:
<asp:DropDownList ID="ddlControl" runat="server" onchange="TriggerChange(this);">
关于javascript - ASP.NET 服务器控件 - 根据下拉选择显示隐藏文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907986/