RUN Alert for ASP LIST ITEMS on dropdownmenu

时间:2015-07-28 16:39:29

标签: javascript html html5

I have the following script. I need to run an alert each time a language is selected from the dropdown menu. I was able to alert it when spanish is selected (value: es), but I was not able to run the other languages alerts by adding the same scripts. Only spanish was alerting.

Here is the script:

<pre>
<b>

<asp:DropDownList ID="aicLanguage" onchange="dropDownListOnChange(this);"  runat="server" name="aicLanguage" Enabled="True">

     <asp:ListItem Value="en" Text="English"></asp:ListItem>
     <asp:ListItem Value="mn" Text="Chinese"></asp:ListItem>
     <asp:ListItem Value="ja" Text="Japanese"></asp:ListItem>
     <asp:ListItem Value="pt" Text="Portuguese"></asp:ListItem>
     <asp:ListItem Value="es" Text="Spanish"></asp:ListItem>
     <asp:ListItem Value="de" Text="German"></asp:ListItem>

</asp:DropDownList> )
<pre>
<b>

     <script type='text/javascript'>
         var dropdown = document.getElementById("aicLanguage");
         dropdown.onchange = function (event) {
             if (dropdown.value == "es") {
                 alert(" SPANISH SCHEDULE")
                }
             }
</script>

1 个答案:

答案 0 :(得分:0)

Just put that into a function

function go() {
   var dropdown = document.getElementById("aicLanguage");
   var pointer = dropdown.value;
   if (dropdown.value == "es") {
       alert(" SPANISH SCHEDULE");

   }
   //More With other values
   //I would consider using switch statement
 }

Then set your drop down to fire that function on change

 <asp:DropDownList onchange="go()" id="aicLanguage">   
     <asp:ListItem Value="en" Text="English"></asp:ListItem>
     <asp:ListItem Value="mn" Text="Chinese"></asp:ListItem>
     <asp:ListItem Value="ja" Text="Japanese"></asp:ListItem>
     <asp:ListItem Value="pt" Text="Portuguese"></asp:ListItem>
     <asp:ListItem Value="es" Text="Spanish"></asp:ListItem>
     <asp:ListItem Value="de" Text="German"></asp:ListItem>
 </asp:DropDownList>

Using switch statement would help

switch(pointer) {
case 'es':
    alert(" SPANISH SCHEDULE");
    break;
case 'en':
    alert("English SCHEDULE");
    break;
default:
    alert("No value defined")
}

Noticed how i added a var pointer inside the function and changed it to the dropdown value soi the switch statement knows what variable to perform the switch on