【发布时间】:2014-10-30 21:42:25
【问题描述】:
帮助解决问题: 在我的 mvc 项目中,我有两个选中的下拉列表(带复选框的下拉列表) 我需要做的是:在第一个下拉列表中选择项目后,根据首先选择的项目过滤(更好地填充)第二个。
我通过 getJSON 获得过滤数据,因为我无法刷新下拉列表。
这是我的代码:
(有类似的问题,我在这里没有找到解决方案https://stackoverflow.com/questions/26239795/how-to-add-multiple-dependent-dropwdowns-chosen-gem)
@Html.DropDownList("divisionIds", (IEnumerable<SelectListItem>)ViewBag.DivisionList, null, new { multiple = "multiple", @class = "multiselect" })
<script type="text/javascript">
//$("#divisionIds").multipleSelect()
$("#divisionIds").chosen()
$("#divisionIds").on('change', function (e) {
//alert($("#divisionIds").val());
var val = $("#divisionIds").val();
var subItems = "";
//alert(val);
$.getJSON("/Home/GetFilteredPostingGroups/" + val, null, function (data) {
$.each(data, function (index, item) {
subItems += "<option value='" + item.Value + "'>" + item.Text + "</option>"
});
alert(subItems);
//here...
$("#postingGroupIds").empty();
$("#postingGroupIds").append(subItems);
$("#postingGroupIds").trigger("chosen:updated");
$("#postingGroupIds").change();
});
});
</script>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
@*@Html.DropDownList("postingGroup")*@
@Html.DropDownList("postingGroupIds", (IEnumerable<SelectListItem>)ViewBag.PostingGroupList, null, new { multiple = "multiple", @class = "multiselect" })
<script type="text/javascript">
$("#postingGroupIds").chosen()
</script>
</td>
<td>
<input type="submit" name="Command" value="@Command.Viewing" />
</td>
</tr>
怎么了? 或者如何以其他方式执行此操作,也许是其他控件?
谢谢!
【问题讨论】:
-
不应用
chosen插件能用吗? -
插件控制不错!问题将在成功回调
data中出现。查看 console.log(data) 中的数据并向我们展示数据的样子。我为此创建了一个小提琴Here is the Working Fiddle Link -
谢谢,文卡塔。很好的样品,我发现了一个问题。当我将我的 javascript 代码放入 $(function) () {...} 时,它开始工作。我不擅长 javascript,请告诉它为什么会这样?
-
我会在回答中添加一个带有建议的描述,以便您更好地理解
标签: jquery asp.net-mvc model-view-controller