简体   繁体   English

如何在$ .getJSON中使用下拉列表值?

[英]How to use dropdown list value inside $.getJSON?

this is my script 这是我的剧本

$.getJSON("Employee.js", function (data) { 
       var sample = data.one;alert(sample) });

and this is Employee.js file 这是Employee.js文件

var sample={ "one":"Manager","two":"Sr.Eng","three":"Eng" }

I am fine with this. 我对此很好。 i want to get the value from drop down list. 我想从下拉列表中获取值。 ie my drop down list will be like follow: 即我的下拉列表将如下所示:

 <asp:DropDownList ID="ddlEmployee" runat="server">
                        <asp:ListItem>one</asp:ListItem>
                        <asp:ListItem>two</asp:ListItem>
                        <asp:ListItem>three</asp:ListItem>
                        <asp:ListItem>four</asp:ListItem>
                    </asp:DropDownList>

i an get the value of ddlEmployee in jscript as 我在jscript中获得ddlEmployee的值

var sel = document.getElementById("<%=ddlEmployee.ClientID%>");
        var opt = sel.options[sel.selectedIndex].text;

Bot how to use "opt" to get the value from Employee.js file? Bot如何使用“ opt”从Employee.js文件中获取值?

You can use bracket notation: 您可以使用方括号表示法:

var opt = 'one';

var sample = { "one":"Manager","two":"Sr.Eng","three":"Eng" };

var val = sample[opt]; // Manager

Try below code : 试试下面的代码:

var data=$.("#ddlEmployee").val();

alert(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM