Javascript 使用用户输入动态创建表
我目前正在尝试使用JS和HTML动态创建一个表 但目前它似乎无法从用户条目中检索值 我做错了什么 提前谢谢Javascript 使用用户输入动态创建表,javascript,Javascript,我目前正在尝试使用JS和HTML动态创建一个表 但目前它似乎无法从用户条目中检索值 我做错了什么 提前谢谢 <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 =
<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>
函数createTable(num_行,numcols)
{
var num_rows=document.tablegen.rows.value;
var num_cols=document.tablegen.cols.value;
变量theader='\n';
var tbody='';
对于函数中的(var i=0;i而不是return
,您需要将表粘贴到文档中,尝试编写如下内容:
相反:
return theader + tbody + tfooter;
写
我不建议使用document.write.read
试试这个:
<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>
函数createTable()
{
var num_rows=document.getElementById('rows').value;
var num_cols=document.getElementById('cols').value;
变量theader='\n';
var tbody='';
对于(变量i=0;ivar fnPuzzel={
最高:9,
民:1,,
rowControl:null,
colControl:null,
初始化:函数(o){
如果(o){
如果($('#'+o.rowControl))
rowControl=$(“#”+o.rowControl);
如果($('#'+o.columnControl))
colControl=$(“#”+o.columnControl);
$(“#”+o.button).bind('click',fnPuzzel.createTable);
}
},
createTable:函数(){
var rows=$(rowControl.val();
var columns=$(colControl.val();
变量表=$('');
对于(var rowCount=0;rowCount0)
$('.container td').not('.selected').eq(randomCell).addClass('random');
}
}
//首先需要导入jquery库
//detail是表的id
$(函数(){
//添加新行
$('.add')。单击(函数(){
var tr=''+
''+
''+
''+
''+
''+
'';
$('.details')。追加(tr);
});
用于(VAR i=0;虽然这个代码片断可以回答这个问题,但它不提供任何上下文来解释为什么或为什么)。考虑添加一两个句子来解释您的答案。对于原始的问题,这个答案加了什么,超出了可接受的答案?原始代码的问题是它不允许您输入T。动态表。这解决了用户输入作为动态表单元格内容的问题,而不是问题中的问题。问题是关于从用户输入导出的表的动态宽度和高度。
<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>
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');
}
}
// 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);
});
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;