简体   繁体   中英

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

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

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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