I have a drop down box, In the controller code I can get the selected id of the drop down item selected. However, what I need is the value that is actually displayed in the drop down box. I had heard that this could be done in JQuery, by getting the selected
variable into a JQuery variable. And then somehow accessing that value in the controller code. Can someone help me out here? Here is my Code so far.
The JQuery code :
$('select#ddlCustomersDropDown').change(function() { var myValue = $("#ddlCustomersDropDown option:selected").text(); alert(myValue); });
var a = document.getElementById("droppdownlistID");
var aValue = a.options[a.selectedIndex].text; And store aValue into Hiddenfield and use this in controller.
Regards, Anj.
Marked as answer by AppDevForMe on Feb 05, 2013 02:27 AM
AppDevForMe
Participant
1396 Points
1327 Posts
How to get a selected value from a drop down box. Not a selected id ?
Feb 04, 2013 04:20 AM|LINK
I have a drop down box, In the controller code I can get the selected id of the drop down item selected. However, what I need is the value that is actually displayed in the drop down box. I had heard that this could be done in JQuery, by getting the selected variable into a JQuery variable. And then somehow accessing that value in the controller code. Can someone help me out here? Here is my Code so far.
The JQuery code :
$('select#ddlCustomersDropDown').change(function() { var myValue = $("#ddlCustomersDropDown option:selected").text(); alert(myValue); });
The drop down box in the view :
<div id="hiddenCustomer" style="display:none;">
<label for="CustomerNameIdentifier" class="required labelLeft" >@Html.LabelFor(model => model.CustomerNameIdentifier)</label> <input name="CustomerNameIdentifier" type="text" />
</div>
<p>
<label for="Customers" class="required labelLeft">@Html.LabelFor(model => model.Customers)</label> @Html.DropDownListFor(model => model.SelectedCustomerID, Model.CustomerSelectList, "--select--", new { @class = "dropDownBox", @id = "ddlCustomersDropDown" })
</p>
Model Code.
public class VisitorCardModel { public string CustomerNameIdentifier { get; set; } [DataType(DataType.Date)] [Display(Name = "Event date")] public DateTime EventDateTime { get; set; }
Rajneesh Ver...
All-Star
37262 Points
6829 Posts
Re: How to get a selected value from a drop down box. Not a selected id ?
Feb 04, 2013 04:32 AM|LINK
Instead of text() use val()
$('select#ddlCustomersDropDown').change(function() { var myValue = $("#ddlCustomersDropDown option:selected").val(); alert(myValue); });www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
goel.ankit
Contributor
2531 Points
513 Posts
Re: How to get a selected value from a drop down box. Not a selected id ?
Feb 04, 2013 09:45 AM|LINK
what version of JQuery are you using?
try this syntax ->
$('#yourdropdownid').find('option:selected').text();Refer ->http://stackoverflow.com/questions/1643227/get-selected-text-from-dropdownlist-using-jquery
Ankit
(Please select 'Mark as Answer' if my response has helped you.)
anj1212
Member
333 Points
74 Posts
Re: How to get a selected value from a drop down box. Not a selected id ?
Feb 04, 2013 10:07 AM|LINK
in javascript :
jp.lima
Member
186 Points
51 Posts
Re: How to get a selected value from a drop down box. Not a selected id ?
Feb 04, 2013 11:44 AM|LINK