选择 DropDownList 值的 Javascript 事件?

标签 javascript jquery asp.net

我有一个 DropDownList,它有两个选项是和否。我现在正在使用 onchange 事件,但第一次加载页面时,它不会被触发,并且下拉列表中的值是 yes。(因为值尚未更改。)我想根据当前值设置控件的可见性下拉列表。

ASPX

<asp:DropDownList ID="ddlAriba" onchange="ddlAribaChange(this);" runat="server"              ClientIDMode="Static" CssClass="FormText" AutoPostBack="True">
    <asp:ListItem Value="Y" Selected='True'>Yes</asp:ListItem>
    <asp:ListItem Value="N">No</asp:ListItem>
</asp:DropDownList>

JAVACRIPT

function ddlAribaChange(ddl) {
    var txtAribaDocumentNumber = document.getElementById("txtAribaDocumentNumber");
    var ddlAribaReason = document.getElementById("ddlAribaReason");
    //alert(ddl.options[ddl.selectedIndex].text);
    if (ddl.options[ddl.selectedIndex].text == 'Yes') {
        txtAribaDocumentNumber.style.display = 'inherit';
        ddlAribaReason.style.display = 'none';
    } else {
        txtAribaDocumentNumber.style.display = 'none';
        ddlAribaReason.style.display = 'inherit';
    }
}

最佳答案

我认为这个问题与页面生命周期有关。 也许使用代码隐藏而不是 javascript 显示/隐藏你的[选择]会更好。

关于选择 DropDownList 值的 Javascript 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23242622/

相关文章:

javascript - Jquery resize() 导致屏幕闪烁和 div 偏移

jquery - MVC 部分 View 使用 jquery.get() 触发 Document.Ready(),但不触发 Ajax.BeginForm

JavaScript Regexp - 允许回车可以让其他不需要的字符验证

javascript - 从函数 ajax 返回

javascript - Google Visualization 突出显示单条网格线

jQuery 验证插件在 IE 中清除文件输入值

c# - HttpListener 问题

c# - webconfig中的属性maxRequestLength是怎么测的

c# - 从所有网页中删除 .aspx

javascript - 访问 IFrame 的 contentWindow.document 在 IE6 上给出 'Access is Denied'