javascript - 如何将下拉列表复制或插入到 html 表格列

标签 javascript jquery html asp.net-mvc

function DisplayADSearchResults(result) {
        var table = "<table id='myTable'>" +
        "<tr>" +
        "<th style='width:20px;'>&nbsp;</th>" +
        "<th style='width:100px;'><b>First Name</b></th>" +
        "<th style='width:100px;'><b>Last Name</b></th>" +
        "<th style='width:200px;'><b>Email</b></th>" +
        "<th style='width:200px;'><b>Login Name</b></th>" +
        "<th style='width:200px;'><b>Role</b></th>" +
        "</tr>";
    for (var counter = 0; counter < result.length; counter++) {
        var chkId = 'chkUserSearch' + counter;
        var fId = 'fUserSearch' + counter;
        var lId = 'lUserSearch' + counter;
        var eId = 'eUserSearch' + counter;
        var unid = 'unUserSearch' + counter;

            var myData = "<tr>" +
            "<td><input type='checkbox' id='" + chkId + "' /></td>" +
            "<td id='" + fId + "' class='searchFirstName'>" + result[counter].FirstName + "</td>" +
            "<td id='" + lId + "' class='searchLastName'>" + result[counter].LastName + "</td>" +
            "<td id='" + eId + "' class='searchEmail'>" + result[counter].EmailAddress + "</td>" +
            "<td id='" + unid + "' class='searchUn'>" + result[counter].LoginName + "</td>" +
            "<td id='" + unid + "' class='searchUn'>" + ddlStaffCategory + "</td>" + //i want do display my "ddlStaffCategory" drop dropdownlist in this column. 
            "</tr>";
        table += myData;
    }
    table += "</table>";}

我有上面的函数,用于根据搜索结果动态创建 html 表。在我的 Razor View 中,我有一个已创建并填充了值的下拉列表。

有人知道如何在上面表格的最后一列中为每一行显示该下拉列表吗?

下面是下拉列表源。

@Html.DropDownList("ddlStaffCategory", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, "Value", "Text"), new { @class = "form-control form-control-dropdown-medium" })

更新

我已经尝试了@ADyson的建议,如下所示(最后两列),但这只是将html打印为纯文本而不是下拉列表控件

var searchData = "<tr>" +
            "<td><input type='checkbox' id='" + chkId + "' /></td>" +
            "<td id='" + dId + "' class='searchDomain'>" + userDomain + "</td>" +
            "<td id='" + fId + "' class='searchFirstName'>" + result[counter].FirstName + "</td>" +
            "<td id='" + lId + "' class='searchLastName'>" + result[counter].LastName + "</td>" +
            "<td id='" + eId + "' class='searchEmail'>" + result[counter].EmailAddress + "</td>" +
            "<td id='" + unid + "' class='searchUn'>" + result[counter].LoginName + "</td>" +
            "<td id='" + unid + "' class='searchUn'>" + "@Html.DropDownList(\"ddlStaffCategory\", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, \"Value\", \"Text\"), new { @class = \"form-control form-control-dropdown-medium\"})" + "</td>" +
            "<td>@Html.DropDownList(\"ddlStaffCategory\", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, \"Value\", \"Text\"), new { @class = \"form-control form-control-dropdown-medium\"})</td>" +
            "</tr>";

最佳答案

@Html.DropDownList(...)只是生成类似的东西:

<select id="SelectId" name="SelectName">
    <option value="Opt1">Option 1</option>
    <option value="Opt2">Option2</option>
    ...
</select>

将你的 Razor 助手放入 <td></td> 中你应该没问题

var myData =
    "<tr>" +
    "<td><input type='checkbox' id='" + chkId + "' /></td>" +
    "<td id='" + fId + "' class='searchFirstName'>" + result[counter].FirstName + "</td>" +
    "<td id='" + lId + "' class='searchLastName'>" + result[counter].LastName + "</td>" +
    "<td id='" + eId + "' class='searchEmail'>" + result[counter].EmailAddress + "</td>" +
    "<td id='" + unid + "' class='searchUn'>" + result[counter].LoginName + "</td>" +
    "<td id='" + unid + "' class='searchUn'>@Html.DropDownList("ddlStaffCategory", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, "Value", "Text"), new { @class = "form-control form-control-dropdown-medium" })</td>" +
    "</tr>";

关于javascript - 如何将下拉列表复制或插入到 html 表格列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46605974/

相关文章:

javascript - &lt;script&gt; 标记在 HTML <pre> 标记中的无效位置

javascript - sinon stub.restore 和 stub.reset 有什么区别

jquery - koGrid 清空我们的网格,而不是使用 Asp.Net MVC 3 使用 KnockoutJS 进行更新

javascript - 在 Hot Towel/Durandal 单页应用程序中加载 View (激活)时调用函数

html - Chrome (33) 显示 inline-block 不同

javascript - 大写第一个字母 - 不能使用 'toUpperCase' (JS)

javascript - 单击按钮添加表行

jquery - Fancybox 中自动调整高度

javascript - 清除所有 setIntervals

HTML注入(inject)到别人的网站?