简体   繁体   English

使用 javascript 在动态 html 表中的每一行末尾创建并附加一个按钮

[英]creating and appending a button to the end of each row in a dynamic html table using javascript

I have a table that I have dynamically created from a JSON object I am getting from the server via an ajax request.我有一个表,它是从我通过 ajax 请求从服务器获取的 JSON 对象动态创建的。 I am trying to append buttons to the end of each of the rows in the table.我正在尝试将按钮附加到表中每一行的末尾。 The table has seven rows, thus I have 7 buttons.该表有七行,因此我有 7 个按钮。 I want something like the following.我想要类似以下的东西。 I want to set a javascript variable to create each individual button with the following parameters :我想设置一个 javascript 变量来使用以下参数创建每个单独的按钮:

(obviously with different ids) (显然具有不同的ID)

        <input type="button"id="submit-button2"class="btn btn primary"value="Submit" />

so that I can set up an array of buttons of these variables in javascript such as the following:这样我就可以在 javascript 中设置这些变量的按钮数组,如下所示:

            var buttons = [
                  button1,
                  button2,
                  button3,
                  button4,
                  button5,
                  button6,
                  button7            

            ]; 

Then I can iterate through my JSON object and the buttons array appending them to the table (named requestTable) like the following :然后我可以遍历我的 JSON 对象和将它们附加到表(名为 requestTable)的按钮数组,如下所示:

       for(var j = 0; j < request[0].DepartmentApprovals.length &&      buttons.length; j++) {

             $("<tr>" + "<td id=\"Departments\">" 

               + request[0].DepartmentApprovals[j].Department +":" + "</td>"

               + "<td input type=\"text\" id=\"ApproverLanId\" contenteditable=\"false\" class=\"underline\">" 

               + request[0].DepartmentApprovals[j].Approver.LanId + "</td>"

               + "<td>" + "Date:" + "</td>"

               + "<td contenteditable=\"false\" class=\"underline\">" + request[0].DateApproved + "</td>"

               + "<td>" +  buttons[j].outerHTML + "</td>"+ "</tr>").appendTo("#requestTable");
        }

The table data works, My issue has been that I am able to get a button to append and show if I use the document.getElementById of an existing button already created elsewhere on the form, but clearly I do not want this, I want them dynamically created with differing ids that exist only in the scope of the table.表格数据有效,我的问题是我能够获得一个按钮来附加并显示我是否使用已经在表单其他地方创建的现有按钮的 document.getElementById,但显然我不想要这个,我想要它们使用仅存在于表范围内的不同 id 动态创建。 I am not sure how to accomplish this through javascript.我不确定如何通过 javascript 完成此操作。

I am not 100% sure if this is what you want, but this code should add a button with a dynamic ID (submit-button-1, submit-button-2, etc ...) inside the last cell of each row:我不是 100% 确定这是否是您想要的,但此代码应该在每行的最后一个单元格内添加一个带有动态 ID 的按钮(submit-button-1、submit-button-2 等...):

for (var j = 0; j < request[0].DepartmentApprovals.length && buttons.length; j++) {
    var appendHTML = '';

    appendHTML += '<tr>';
    appendHTML += '<td id="Departments">' + request[0].DepartmentApprovals[j].Department + ':</td>';
    appendHTML += '<td input type="text" id="ApproverLanId" contenteditable="false" class="underline">' + request[0].DepartmentApprovals[j].Approver.LanId + '</td>';
    appendHTML += '<td>Date:</td>';
    appendHTML += '<td contenteditable="false" class="underline">' + request[0].DateApproved + '</td>';
    appendHTML += '<td><input type="button" id="submit-button-' + (j + 1) + '" class="btn btn primary" value="Submit" /></td>';
    appendHTML += '</tr>';

    appendHTML.appendTo('#requestTable');
}

try this尝试这个

var button = document.createElement("button");
button.innerHTML ="/*put name of button here*/";

// 2. Append somewhere
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);

// 3. Add event handler
button.addEventListener ("click", function(){/*put what button does here*/}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM