Reputation: 149
Here I have dynamic dropdowns based on provider. I have two providers example providerid=15 and providerid=16. Whenever I am selecting dropdown of providerid=15 I am getting selected dropdown value of that one but whenever I am selecting dropdown of providerid=16 I am getting static option which I kept i.e.,"Avaliability" is coming. I dont want that static value I want providerid=16 dropdwon selected value.
This is my code:
@if (provider.lstSession.Count > 0)
{
<div class="form-group" id="svcn">
<div style="color:red"> @ViewBag.Message</div>
<select id="SessionID" name="SessionID" class="form-control">
<option>Avaliablity</option>
@foreach (var Sesssion in provider.lstSession)
{
if (provider.ProviderID == Sesssion.ProviderID)
{
<option value="@Sesssion.ProviderID'_'@Sesssion.SessionID" id="sss">@Sesssion.SessionName</option>
}
}
</select>
</div>
}
$("#SessionID").change(function (){
var slotvalue = $('#SessionID :selected').val();
});
Upvotes: 0
Views: 55
Reputation: 152
Will
$(this).val()
not work? Also inspect the drop down via Dev tools, try to get a jsfiddle created with generated html and no server side code for others to verify.
Upvotes: 1
Reputation: 41
You need to put "this" to get the actual selected value, not the first one selected:
$("#SessionID").change(function (){
var slotvalue = $('#SessionID :selected', this).val();
});
or
$("#SessionID").change(function (){
var slotvalue = $(this).find('option:selected').val();
});
Upvotes: 0