Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用jQuery填充DropDownList_Javascript_Jquery_Asp.net Mvc_Html.dropdownlistfor - Fatal编程技术网

Javascript 用jQuery填充DropDownList

Javascript 用jQuery填充DropDownList,javascript,jquery,asp.net-mvc,html.dropdownlistfor,Javascript,Jquery,Asp.net Mvc,Html.dropdownlistfor,如何使用jQuery填写DropDownList 我打电话给我的控制器: $.getJSON('@Url.Action("GetCategories")', { groupId: selectedGroup }, function (categories) { 这将返回类类别中的对象: public class Category { public int Id { get; set; } public int GroupId { get; set; }

如何使用jQuery填写DropDownList

我打电话给我的控制器:

$.getJSON('@Url.Action("GetCategories")', { groupId: selectedGroup }, function (categories) {
这将返回类类别中的对象:

  public class Category
  {
      public int Id { get; set; }
      public int GroupId { get; set; }
      public string Description { get; set; }
  }
如何从DropDownList中的Category中获取Id值,并从文本中的Category中获取描述

这不管用

$.each(categories, function (index, category) {
categoriesSelect.append($('<option/>', {
    value: category.Id,
    text: category.Description
 }));
});
$。每个(类别、功能(索引、类别){
分类选择。追加($(''){
值:category.Id,
文本:类别。说明
}));
});
这不起作用,它会显示消息“类别未定义”

这是类别中的内容:

试着这样做

$.get('http://webapi.com' , function (res) {
    if (res) 
    {  
        if (res.customers && res.customers.length > 0) 
        {
            $.each(res.customers, function (id, cus) {
                $("#customers-dropdown").append($('<option></option>').val(cus.id).html(cus.name));
            });
        }
    }
}); 
$.get('http://webapi.com,函数(res){
如果(res)
{  
if(res.customers&&res.customers.length>0)
{
美元。每个(客户、功能(id、cus){
$(“#客户下拉列表”).append($('').val(cus.id.html(cus.name));
});
}
}
}); 
给你:

categoriesSelect.append($("<option></option>")
    .attr("value", category.Id)
    .text(category.Description));
categoriesSelect.append($(“”)
.attr(“值”,category.Id)
.文本(类别、说明);

我犯了一个小错误,它应该是category.id而不是category.id,category.description而不是category.description。

我注意到在您的问题中,您在ajax中收到的类别表是对象:

 public class Category
  {
      public int Id { get; set; }
      public int GroupId { get; set; }
      public string Description { get; set; }
  }
答案中的属性会更改
Id->Id
<代码>组ID->组ID<代码>说明->说明。似乎有一个从第一个属性字母到小写的转换。在建立下拉列表时,请将其考虑在内

这段代码可以做到这一点。此处的category数组包含响应格式的对象,该响应称为调用Ajax:

$(函数(){
//这是一幅猫的画面,在阿贾克斯的舞台上
变量类别=[
{
id:1,
groupId:“Gp1”,
说明:“1类”
},  
{
id:2,
groupId:“Gp1”,
说明:“第2类”
},  
{
id:3,
groupId:“Gp1”,
说明:“第3类”
},  
{
id:4,
groupId:“Gp1”,
说明:“第4类”
},  
{
id:5,
groupId:“Gp1”,
说明:“第5类”
}];
//在西农的巡回演出中,阿贾克斯歌剧院的歌剧院上演了一幕
$(“#myDynamicSelect”).html(“”);
//这是一个很好的例子,它是一个“类别”表,它是一个阿贾克斯表
$。每个(类别,功能(i,类别){
//给我一杯茶,你的名字叫“我的活力之选”
var选项=新选项(category.description,category.id);
$(“#myDynamicSelect”)。附加(选项);
});   
});


Debug。设置断点并查看
类别中的实际内容。或者使用
console.log(JSON.stringify(categories,null,2))
查看您的服务实际得到了什么。@HereticMonkey用调试截图更新了我的问题这是否回答了您的问题?您将其作为一个答案编写,但它似乎无法回答您为什么会收到一条关于类别未定义的消息…不知道,但这似乎有效,该消息是因为我没有按f11继续我猜。此答案还可以帮助您: