0

我正在使用外部JavaScript文件,当未选择下拉列表值时,它会给我一条消息。它默认设置为“选择”。我在搜索过程中尝试了很多东西,但它并未在我的网站上运行。我正在使用4.5版框架工作的Visual Studio 2013。使用Javascript在asp.net 4.5中的Dropdownlist选定值

下面是我试过的代码。

function unicollege() 
{ 

    var ddlObject = document.getElementById("<%=ddlType.ClientID%>"); 
    var selectedValue = ddlObject.options[ddlObject.selectedIndex].value; 

    var e = document.getElementById("ddlLocation"); 
    var selectedLocation = e.options[e.selectedIndex].value; 

    // .option and .value property are not available in my program. 

} 

回答

0

这里是需要在你的页面中使用外部JavaScript文件:

 protected void Page_Load(object sender, EventArgs e) 
    { 
     ScriptManager.RegisterClientScriptInclude(
     this, 
     typeof(Page), 
     "", 
     ResolveClientUrl("JavaScript1.js")); 
    } 

在你的ASPX标记使用一个变量为您DDL来在外部JavaScript文件中引用:

<script type="text/javascript"> 
     var ddl = "<%=ddlList.ClientID%>"; 
    </script> 

,并在外部JavaScript文件:

/// <reference path="WebForm1.aspx" /> 

    function unicollege() { 

    var ddlObject = document.getElementById(ddl); 
    var selectedValue = ddlObject.options[ddlObject.selectedIndex].value; 
    } 
+0

我已经尝试过,但它不工作。还有一件事我想告诉你。我的Java脚本工作,如果我把它放入asp.net文件,但是当我把它放在extenal JavaScript文件中不起作用。我希望这可以帮助你理解我的问题。 – Muryali

+0

你正在击中.js文件中的断点吗? – Gregg

0

你真的不需要考虑这么复杂。 我每天做这类工作一千次。

function unicollege() 
{ 
    var ddlSelectedValue = document.getElementById("ddlType").value; 
} 

这个我刚刚给ütesting..it会后完全正常工作,我做了很长一段time.however要小心回发的问题,如果他们发生。

相关问题