我有一个选择,我想用json数据结果填充,但数据显示为“未定义”。根据情况,当按下按钮时,根据所选ID的数据将被排序到下拉列表中。
<select id="myDropDown" name="myDropDown"></select>
function bindItems(MarkaID) {
var MarkaID = 136; //entering data manually
$.ajax({
type: 'POST',
dataType: "json",
data: JSON.stringify({ MarkaID: MarkaID }),
contentType: "application/json; charset=utf-8",
url: '/Home/GetUrunCinsleriForMarka'
}).done(function (jsonObj) {
var listItems = "";
for (i in jsonObj) {
listItems += "<option value='" + jsonObj[i].Key + "'>" + jsonObj[i].Value + "</option>";
}
$("#myDropDown").html(listItems);
});
}
[HttpPost]
public JsonResult GetUrunCinsleriForMarka(int MarkaID)
{
List<SqlParameter> sqlparameters = new List<SqlParameter>();
sqlparameters.Add(new SqlParameter("@MarkaID", MarkaID));
var urunCinsList = db.Database.SqlQuery<UrunCinsleriForMarka>("SP_GetUrunCinsleriForMarka @MarkaID", sqlparameters.ToArray());
return Json(urunCinsList, JsonRequestBehavior.AllowGet);
}
像这样采样数据ID和值
96 | Power Supply
71 | Cameras
6 | Alarm Devices
118 | Mouse
11 | Fever-measuring devices
58 | Kettle
59 | ..............
137 | ..............
139 | ..............
答案 0 :(得分:0)
我为您的问题制作了示例代码。请输入以下代码:
HTML代码:
<select id="questionNumber" class="form-control">
<option>Select Number</option>
</select>
jQuery代码:
var result = {data:[
{
id:"5",
number: "5"
},
{
id: "89",
number: "89"
}
]}
$.each(result.data, function (key, value) {
$("#questionNumber").append($('<option>', {
value: value.id,
text: value.number,
'data-mark': value.id
}));
});
此处的JSFIddle链接: