gpt4 book ai didi

javascript - 重置元素后刷新下拉列表

转载 作者:行者123 更新时间:2023-11-30 13:37:43 24 4
gpt4 key购买 nike

我有一个 asp.net mvc 2 应用程序,并且有一些 jquery 定义了两个下拉列表的行为。当一个发生变化时,另一个将填充过滤后的数据。狂怒之后,我让 jquery 工作,通过 Firebug 调试确认,但我的下拉列表没有刷新。这是 jquery

<script type="text/javascript">

$(function () {
$('#cid').change(function () {
var coid = $(this).val();
$.post("/TimeTracking/FilterFieldOffices", { companyId: coid }, function (data) {
$("#foid").loadSelect(data);
});
});
});

$(function () {
$.fn.loadSelect = function (data) {
return this.each(function () {
this.options.length = 0;
$.each(data, function (index, itemData) {
var option = new Option(itemData.Text, itemData.Value);
this.add(option);
});
});
};
});

</script>

这是我的 Controller 操作

public ActionResult FilterFieldOffices(int companyId = 0)
{
IList<FieldOffice> list = _fodp.GetFieldOfficesForCompany(companyId);

var returnList = new SelectList(list, "Id", "FacilityName");

return Json(returnList);
}

所以,我知道下拉列表正在填充正确的数据,但 View 页面上的下拉列表没有刷新。我对 JQuery 的了解有限,所以如果我遗漏了一些 n00b 之类的东西,请保持温和。

最佳答案

试试这个:

$(function () {
$.fn.loadSelect = function (data) {
return this.each(function () {
this.options.length = 0;
var select = this;
$.each(data, function (index, itemData) {
var option = new Option(itemData.Text, itemData.Value);
$(select).append(option);
});
});
};
});

请注意,我们需要在外部 foreach 中捕获 this,因为在内部它不再指向 select 元素。

完整的工作示例:

型号:

public class Item
{
public int Value { get; set; }
public string Text { get; set; }
}

public class MyViewModel
{
public int? SelectedCompanyId { get; set; }
public int? SelectedFieldOfficeId { get; set; }
public IEnumerable<Item> Companies { get; set; }
public IEnumerable<Item> FieldOffices { get; set; }
}

Controller :

[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
Companies = Enumerable.Range(1, 5).Select(i => new Item
{
Value = i,
Text = "Company " + i
}),
FieldOffices = Enumerable.Empty<Item>()
};
return View(model);
}

public ActionResult FilterFieldOffices(int companyId)
{
return Json(Enumerable.Range(1, 3).Select(i => new Item
{
Value = i,
Text = "Field offfice " + i
}));
}
}

查看:

<script type="text/javascript" src="<%= Url.Content("~/scripts/jquery-1.4.1.js") %>"></script>
<script type="text/javascript">
$(function () {
$('#cid').change(function () {
var coid = $(this).val();
$.post('<%= Url.Action("FilterFieldOffices") %>', { companyId: coid }, function (data) {
$('#foid').loadSelect(data);
});
});
});

$(function () {
$.fn.loadSelect = function (data) {
return this.each(function () {
this.options.length = 0;
var select = this;
$.each(data, function (index, itemData) {
var option = new Option(itemData.Text, itemData.Value);
$(select).append(option);
});
});
};
});

</script>

<% using (Html.BeginForm()) { %>
<%: Html.DropDownListFor(x => x.SelectedCompanyId, new SelectList(Model.Companies, "Value", "Text"), new { id = "cid" })%>
<%: Html.DropDownListFor(x => x.SelectedFieldOfficeId, new SelectList(Model.FieldOffices, "Value", "Text"), new { id = "foid" })%>
<input type="submit" value="OK" />
<% } %>

关于javascript - 重置元素后刷新下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3965283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com