MVC3的DropDownList + JSON +选中的问题 [英] MVC3 DropDownList + JSON + selected issue

查看:143
本文介绍了MVC3的DropDownList + JSON +选中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网站(MVC3 + JQuery的)和它的移动版本(MVC3 + jQuery Mobile的),这是在两个不同的解决方案之间的分裂一个很奇怪的行为。

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.

我用下面的code根据另一个(搜索项)更改下拉(SearchValue)的concent。

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

剃刀:

@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));
                        });
                    }
                });

这code是工作在我的网站(JQuery的)完美的罚款,但它不是我的手机网站(jQuery Mobile的),在那里工作的Opera Mobile但不是FF或IE或Safari的情况下...(我与用户代理试图模仿移动设备)

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)

SearchValue下拉的值正确地设置,但在JSON呼叫结束时,所选择的值是previous选择的值(这是不是在下拉的可能值)。我不知道是明确的,所以我给你一个小例子:

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

当我选择的名称,我想在SearchValue类似名1,名称2,等等等等,显示
当我选择汽车,我想在SearchValue类似分享帮助显示,CAR2,等等等等。

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

但随着我的移动网站上的previous code,当我改变值名到汽车分享帮助,CAR2等,DropDownList的正确与正确的价值观(重新填充 ),但所选择的值是previous一项(如名1),即使它不是在下拉的可能值我也尝试手动选择的第一个值,但没有任何反应:

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:对不起,我英文不好

PS: Sorry for my poor english

推荐答案

好吧,我找到了解决办法。我必须刷新下拉列表中,奇怪!
这样的:

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

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

<一个href=\"http://www.andymatthews.net/read/2011/12/14/Refreshing-jQuery-Mobile-listviews,-buttons,-select-dropdowns,-and-input-fields\"相对=nofollow>来源

这篇关于MVC3的DropDownList + JSON +选中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆