Get SelectedIndex from DropDownList - ASP .NET MVC 2 from Javascript

2

I have a page with MVC 2 , which I had in .aspx , Javascript code, but since I need to use that page from a < em> widget native to iOS , I want to analyze an XML . I had to remove the Javascript code to a separate file because the && , > and < do not analyze it correctly, and it does not load the page.

I want to get the value of a DropDownList that is in .aspx from the .js file.

The DropDownList I have it like this:

<%= Html.DropDownList(
    Model.RegistrationDocumentTypeKey,
    Model.GetDocumentTypes(),
    new {
        @id = Model.RegistrationDocumentTypeKey,
        onchange = "onDocumentTypeChangeAux()",
        selectedIndex = 1
    }
)%>

I would like to obtain the value of the selected index from the onDocumentTypeChangeAux() method.

Before moving the Javascript code to the file, it was like this, and it worked:

var ddl = document.getElementById('<%=  Model.RegistrationDocumentTypeKey %>');
switch (ddl.options[ddl.selectedIndex].value) ...

But now, this does not work, I tried to change it by only putting the id , doing it as with jQuery , but I have not succeeded.

    
asked by Stephany 18.01.2017 в 22:15
source

1 answer

0

You can try the following method to get the dropdownlist Id:

$('#tusDropDownList').change(function () { /* Esto es el evento del cambio de ddl */     
    /* Obtener el valor de tus dropdownlist */
    var selectedId = $(this).val();
});

I hope this helps ...

    
answered by 08.02.2017 в 11:27