为什么我的渲染元素在select标记之外?

时间:2015-06-18 13:41:01

标签: javascript jquery html

Screenshot

我正在尝试将html附加到对话框中的内容持有者,正如您在图像中看到的那样,Mile,Meter和Kilometer不在选择下拉列表中。这是为什么?

var rows = $('#jqxUOMRelatedUnitsDropdownGrid').jqxGrid('getrows');
$("#divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder").html('<select id="listNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder">');
for (var i = 0; i < rows.length; i++) {
  var row = rows[i];
  if (row.UOMRelatedUnit_AddItem) {
    $("#divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder").append("<option value='" + row.UOMRelatedUnit_Name + "'>" + row.UOMRelatedUnit_Name + "</option>");
  }
}
$("#divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder").append("</select>");
<div id="divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder"></div>

1 个答案:

答案 0 :(得分:3)

因为您要附加到<div>而不是<select>。这样做:

$("#divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder select").append("<option value='" + row.UOMRelatedUnit_Name + "'>" + row.UOMRelatedUnit_Name + "</option>");
// --------------------------------------------------- ^^^^^^

你做不到:

$("#divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder").append("</select>");

那是无效的!将所有内容存储在tempAppend字符串中,然后使用:

$("#divNewUnitOfMeasureDefaultUnitsPurchasePlaceHolder").append(tempAppend);