3

I have a very strange behaviour between my website (MVC3 + JQuery) and its mobile version (MVC3 + JQuery mobile) which are split in two distinct solutions.

I am using the following code to change the concent of a dropdown (SearchValue) according to another one (SearchBy).

Razor:

@Html.DropDownListFor(x => x.SelectedSearch, new SelectList(Model.SearchBy, "key", "value"), new { onchange = "GetValues($(this).val());" })
@Html.DropDownListFor(m => m.SelectedSearchValue, new SelectList(Model.SearchValue, "key", "value"))

JSON:

$.ajax({
                    type: "POST",
                    url: '@Url.Action("GetValues", "Search")',
                    data: "{searchId:" + searchId + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var options = $('#SelectedSearchValue');
                        $('option', options).remove(); // Remove previous

                        $.each(data, function () {
                            options.append($('<option/>').val(this.Id).text(this.Display));
                        });
                    }
                });

This code is working perfectly fine on my website (JQuery) but it is not the case on my mobile website (JQuery mobile) where it is working for Opera Mobile but not FF or IE or Safari ... (I tried with the User agent to simulate mobile devices)

The values of the SearchValue dropdown are correctly set but at the end of the JSON call, the selected value is the previous selected value (which is not in the possible values of the dropdown). I am not sure to be clear so I give you a little example :

I have the following possibilities for SearchBy: - Name - Car

When I select Name, I would like to display in SearchValue something like "Name1", "Name2", etc etc And when I select Car, I would like to display in SearchValue something like "Car1", "Car2", etc etc

But with the previous code on my mobile website, when I change the value "Name" to Car", the dropdownlist is correctly refilled with the right values ("Car1", "Car2", etc) but the selected value is the previous one (e.g. "Name1") even if it is not in the possible values of the dropdown. I also try to select manually the first value but nothing happens :

$('option[value=' + myBeautifulValue + ']').attr('selected', 'selected');

Do you have an idea to fix my problem? Is what I am doing wrong?

Thank you in advance for your help, Nico

PS: Sorry for my poor english

2 Answers 2

3

Ok, I found the solution. I have to refresh the dropdown, wierd! Like that :

var myselect = $("#SelectedSearchValue"); 
myselect[0].selectedIndex = 0; 
myselect.selectmenu('refresh'); 

Source

Sign up to request clarification or add additional context in comments.

Comments

1

Try using jQuery's prop:

$('option[value=' + myBeautifulValue + ']').prop('selected', true);

See jQuery upgrade blog notes — when setting the selected attribute on an element use .prop to get/set the value.

4 Comments

Unfortunately it is not working. But I will use 'prop' now because it is preferred usage :) Thnks for your idea!
Ok, I found the solution here. I have to refresh the dropdown, wierd! Like that : var myselect = $("#SelectedSearchValue"); myselect[0].selectedIndex = 0; myselect.selectmenu('refresh'); [andymatthews.net/read/2011/12/14/… [1]: andymatthews.net/read/2011/12/14/…
@nFournier that is new to me, you should answer your own question as it may help someone else having the same issue ...
I was not able to do it because I got not enough reputation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.