开发者

How do i add input fields (textbox) to a table dynamically created with jQuery

开发者 https://www.devze.com 2023-03-25 03:35 出处:网络
I have been playing with Javascript to find out how i can dynamically create textboxes and a dynamic table. I figured out how to create the textboxes and the table, but i dont know how to merge theese

I have been playing with Javascript to find out how i can dynamically create textboxes and a dynamic table. I figured out how to create the textboxes and the table, but i dont know how to merge theese together, so that the dynamically created table holds the textboxes with my preferred parameters.

I have this jQuery script which dynamically adds rows and cells to a table. I specify how many rows the table should have in an input field called "element"

The HTML:

<input type="text" name="element" id="element" />
    <input type="button" value="Tilføj" id="add" />
    <br />

    <span id="MCQ-textbox">&nbsp;
        <table id="tbl" border="1">
            <tbody>
            </tbody>
        </table>
    </span>

The scripts:

<scripts language="javascript">

function createDynamicTable() 
{
    var tbody = $("#tbl");
    var rows = $('#element').val();
    var number_of_columns = 3;

    if (tbody == null || tbody.length < 1) return;

    var tfirstrow = $("<tr>");
    $("<th>")
                .addClass("tableCell")
                .text("#")
    //               .data("col")
                .appendTo(tfirstrow);
    $("<th>")
                .addClass("tableCell")
                .text("Svarmulighed")
    //               .data("col")
                .appendTo(tfirstrow);
    $("<th>")
                .addClass("tableCell")
                .text("Hjælpetekst")
    //               .data("col")
                .appendTo(tfirstrow);

    tfirstrow.appendTo(tbody);

    for (var i = 0; i < rows; i++) {

        var trow = $("<tr>");
        for (var c = 0; c < number_of_columns; c++) 
        {
            var cellText = "Cell"
            $("<td>")
                    .addClass("tableCell")
                    .text(cellText)
                    .data("col", c)
                    .appendTo(trow);
        }
        trow.appendTo(tbody);
    }

}


$(document).ready(function () {
    $('#add').click(function () {
        createDynamicTable();
    });
});

</SCRIPT>

Instead of adding the text "cell" to each column, i would like to add textboxes.

The first column should have the following attributes:

var element = document.crea开发者_高级运维teElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "MCQ_"+i+"__choice_number");
    element.setAttribute("name", "MCQ["+i+"].choice_number");

the second column should have the following attributes:

 var element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "MCQ_"+i+"__choice_wording");
    element.setAttribute("name", "MCQ["+i+"].choice_wording");

and the third column should have the following attributes:

    var element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "MCQ_"+i+"__help_text");
    element.setAttribute("name", "MCQ["+i+"].help_text");

How do i merge this, so that my table holds the textboxes with the parameters above?

Thanks!


This will work: http://jsfiddle.net/RrhT9/

Also, you could have added content to the cell your way, but you'd have to replace .text("<input.../>") with .html("<input.../>)


You can do the following:

var cellText = "<input type="text" id=\"MCQ_"+i+"__choice_number\" name=\"MCQ["+i+"].choice_wording\"/>";

And then change accordingly for each of the input boxes you're creating.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号