Selected option of dropdown not changing after being set Jquery

302
November 21, 2016, at 7:27 PM

I have a dropdown list that is generated in C#

@Html.DropDownList("State", Model.States, new { id = "BTState" }, Model.GetDisabled("State"))

I noticed that after I select a different option, the selected value is not changing, even though it is displaying the option I selected. For example, when the page loads the value is "RI"; if I change it to "MA", the dropdown says "MA", but when I submit the form the value that is submitted is "RI" and when I inspect the dropdown in Chrome, I can see that "RI" is selected.

I thought maybe I could fix this by setting the selected option manually, so I wrote this:

$("select").on("change", function () {
    var state = $("option:selected", this).text();
    $("option", this).filter(function () {
        //may want to use $.trim in here
        return $(this).text() == state;
    }).prop('selected', true);
});

It seemed to be working; when I step through, 'state' is the option that I selected, and at the end of the method when I hover over 'this' and inspect the options, "MA" has "selected=true" and "RI" has "selected=false". However, when I inspect the select box, RI is still selected:

<option selected="selected" value="RI">RI</option> 
READ ALSO
JSONP without ?jsoncallback=?

JSONP without ?jsoncallback=?

I'm just diving into JSON and found the following. json file from data.

433
Update existing list to match new list

Update existing list to match new list

I have tree-like list that can be collapsed or expanded by the user.

282
jQuery parallax background image skips on first scroll

jQuery parallax background image skips on first scroll

I have implemented a simple jQuery parallax script into my site. When a div has the class 'parallax' and the following data data-type="background" data-speed="8" the script that handles the parallax motion will act.

386
Add class to a Dynamic Row using Javascript

Add class to a Dynamic Row using Javascript

I am using a script to add a row to my form table which is working absoulty fine. On my first input box I am using a datepicker which is working on the first row but when I add another row the datepicker isnet working.

424