javascript - How to dropdown bind according to this record
问题描述
I have two loops:
- table loop: fills the table rows
- dropdown loop: fills the dropdowns with
typeid.data[i].TypeId
, add a select in the last dropdown
My dropdown does not fill according to its record and I don't understand why.
var QuestionId = data[i].QuestionOid;
var fkid = data[i].FkSource;
var selectdata = data[i];
var selectinnerhtml = "<span><select id = \"answer" + QuestionId + "\" name = \"answer" + QuestionId + "\" class=\"answer" + QuestionId + " form-control input-small\" > </select></span>";
$.ajax({
type: "GET",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
url: '/MYaPI/EmployeeDetails/' + data[i].TypeId,
success: function(datasa) {
var optionhtmls = '<option value="' +
0 + '">' + "--Select--" + '</option>';
$(".answer" + QuestionId).append(optionhtmls);
$.each(datasa, function(j) {
var optionhtmls = '<option value="' +
datasa[j].Oid + '">' + datasa[j].Title + '</option>';
$(".answer" + QuestionId).append(optionhtmls);
});
}
});
var newRows2select = "<tr class='rows'><a href = '' >" +
" <td QuestionCategoryTitle = " + selectdata.QuestionCategoryTitle + " QuestionHeader = " + selectdata.QuestionHeader + " ContentTypeId=" + selectdata.FkSource + " QuestionTypeId=" + selectdata.FkQuestionType + " QuestionOID=" + selectdata.QuestionOid + " CategoryOID=" + selectdata.FkQuestionCategory + " class=\"question-block\"><small style=\"color:slateblue;font-weight: bolder;display:none\">CATEGORY: " + selectdata.QuestionCategoryTitle + ",</small>" +
" <i class=\"deleteRow fas fa-trash float-right\"></i> " +
"<p> " + selectdata.QuestionHeader + "</p>" + selectinnerhtml + " </td></a> \"</tr>";
$("#table23").append(newRows2select);
解决方案
我不知道 jQuery 方式,但是使用纯 JavaScript 很容易实现
var select = document.createElement("select");
var selectinnerhtml = document.createElement("span");
selectinnerhtml.appendChild(select);
$.each(datasa, function(j) {
//create a new option element
var option = document.createElement("option");
//set the value attribute
option.setAttribute("value", datasa[j].oid);
//fill the HTML tag
option.value = datasa[j].Title
//add the option to the select dropdown
select.add(option);
});
推荐阅读
- python - groupby 函数 pandas 数据框中不包含零行
- powershell - 有没有办法在 powershell 中调用带有客户端和服务证书的 Web 服务?
- vb.net - 线程和模态窗体窗口
- javascript - javascript reduce 使用 reduce/map 函数将平面数组转换为嵌套数组
- typescript - 如何在 Quasar/VueJS 中使用 npm TypeScript 包
- javascript - 如何使用 HTML 5 Drag And Drop API 为被拖动的项目设置样式
- dictionary - 删除地图中的网格线并在地图中进行平滑处理
- python - 如何制作一个将其点连接到其最近邻居的图?
- plugins - 更新 3.06 的 unicode 插件
- python - Scrapy:为什么我不能从地下天气中提取目标数据?