Javascript 使用用户输入动态创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8182608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:00:34  来源:igfitidea点击:

Dynamically creating table with user input

javascript

提问by methuselah

I'm currently trying to dynamically create a table using JS and HTML.

我目前正在尝试使用 JS 和 HTML 动态创建一个表。

But at the moment it cannot seem to retrieve the values from user entry.

但目前它似乎无法从用户条目中检索值。

What I am I doing wrong?

我做错了什么?

Thanks in advance!

提前致谢!

<script type="text/javascript">
function createTable(num_rows, numcols)
{
    var num_rows = document.tablegen.rows.value;
    var num_cols = document.tablegen.cols.value;
    var theader = '<table>\n';
    var tbody = '';

    for( var i=0; i<num_rows;i++)
    {
        // create each row
        tbody += '<tr>';

    for( var j=0; j<num_cols;j++)
    {
        // create cell
        tbody += '<td>';
        tbody += 'Cell ' + i + ',' + j;
        tbody += '</td>'
    }

    // closing row table
    tbody += '</tr>\n';

    }

    var tfooter = '</table>';

    // TO DO

    return theader + tbody + tfooter;
}
</script>
</head>

<body>
<form name="tablegen">
<label>Rows: <input type="text" name="rows"/></label><br />
<label>Cols: <input type="text" name="cols"/></label><br/>
<input name="generate" type="button" value="Create Table!" onclick='createTable();'/>
</form>
<script type="text/javascript">
document.write(createTable());
</script>

回答by Book Of Zeus

I would not recommend to use document.write. read this

我不建议使用 document.write。读这个

Why is document.write considered a "bad practice"?

为什么 document.write 被认为是“不好的做法”?

Try this:

尝试这个:

<script type="text/javascript">
function createTable()
{
    var num_rows = document.getElementById('rows').value;
    var num_cols = document.getElementById('cols').value;
    var theader = '<table border="1">\n';
    var tbody = '';

    for( var i=0; i<num_rows;i++)
    {
        tbody += '<tr>';
        for( var j=0; j<num_cols;j++)
        {
            tbody += '<td>';
            tbody += 'Cell ' + i + ',' + j;
            tbody += '</td>'
        }
        tbody += '</tr>\n';
    }
    var tfooter = '</table>';
    document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;
}
</script>
</head>

<body>
<form name="tablegen">
<label>Rows: <input type="text" name="rows" id="rows"/></label><br />
<label>Cols: <input type="text" name="cols" id="cols"/></label><br/>
<input name="generate" type="button" value="Create Table!" onclick='createTable();'/>
</form>

<div id="wrapper"></div>

You can also use the insertRowand insertCell.

您还可以使用insertRowinsertCell

read more:

阅读更多:

https://developer.mozilla.org/en/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

https://developer.mozilla.org/en/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

http://www.w3schools.com/js/tryit.asp?filename=try_dom_table_insertrow

http://www.w3schools.com/js/tryit.asp?filename=try_dom_table_insertrow

回答by antyrat

Instead of returnin your function you need to paste table to the document try to write something like this:

return您需要将表格粘贴到文档中,而不是在您的函数中尝试编写如下内容:

instead:

反而:

return theader + tbody + tfooter;

write

document.write(theader + tbody + tfooter)

回答by Praveen Kumar Burman

var fnPuzzel = {
max: 9,
min: 1,
rowControl: null,
colControl: null,
init: function (o) {
    if (o) {
        if ($('#' + o.rowControl))
            rowControl = $('#' + o.rowControl);
        if ($('#' + o.columnControl))
            colControl = $('#' + o.columnControl);
        $('#' + o.button).bind('click', fnPuzzel.createTable);
    }
},
createTable: function () {
    var rows = $(rowControl).val();
    var columns = $(colControl).val();
    var table = $('<table>');
    for (var rowCount = 0; rowCount < rows; rowCount++) {
        var tr = $('<tr>');
        for (var columnCount = 0; columnCount < columns; columnCount++) {
            $(tr).append($('<td>'));
        }
        $(table).append(tr);
    }
    $('.container').empty().append($(table).html()).find('td').bind('click', fnPuzzel.selectColumn);
    var delay = parseInt(Math.random() * 3000);
    setTimeout(fnPuzzel.selectRandom, delay);
},
selectColumn: function () {
    var cell = this;
    if (!$(cell).hasClass('selected'))
        $(cell).addClass('selected');

},
selectRandom: function () {
    var mx = $('.container td').not('.selected').length;
    var mn = 0;
    var randomCell = mn + Math.floor(Math.random() * (mx + 1 - mn));
    if (randomCell > 0)
        $('.container td').not('.selected').eq(randomCell).addClass('random');
}
}

回答by keerthu sunil

for( var i=0; i<num_rows;i++)
{
    tbody += '<tr>';
    for( var j=0; j<num_cols;j++)
    {
        tbody += '<td>';
        tbody += '<input type="text">';// to display or type content
        tbody += '</td>'
    }
    tbody += '</tr>\n';
}
var tfooter = '</table>';
document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;

}

}

回答by Franklin CI

// first you need to import jquery library              
// detail is id of the table 

$(function(){

// add new row 
$('.add').click(function(){
  var tr = '<tr>'+
               '<td><input type="text" name="product[]" required class="form-control product" ></td>'+
               '<td><input type="text" name="quantity[]" required class="form-control quantity"></td>'+
               '<td><input type="text" name="product[]" required class="form-control product" ></td>'+
               '<td><input type="text" name="amount[]" required class="form-control amount"></td>'+
               '<td><input type="button"  class="btn btn-danger remove" value="-"></td>'+
          '</tr>';
  $('.details').append(tr);

});