【问题标题】:How to dropdown bind according to this record如何根据此记录进行下拉绑定
【发布时间】:2019-03-21 12:47:05
【问题描述】:

我有两个循环:

  • 表格循环:填充表格行
  • 下拉循环:用typeid.data[i].TypeId填充下拉列表,在最后一个下拉列表中添加一个选择

我的下拉列表没有根据其记录填写,我不明白为什么。

  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);

【问题讨论】:

    标签: javascript jquery ajax dom-events


    【解决方案1】:

    我不知道 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);
    });
    

    【讨论】:

    • 我认为它显示了错误的结果,因为在我们添加第二个下拉菜单后自动下拉选项绑定第二个而不是第一个
    • 你能澄清一下吗?
    猜你喜欢
    • 2015-09-03
    • 2018-10-19
    • 1970-01-01
    • 2014-05-05
    • 2019-04-22
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多