在javascript中动态生成表单
嘿大家 尝试生成一个将进入表格单元格的单按钮表单。问题是这需要在 JavaScript 中动态生成。该表还有几个其他值。我首先在没有表单部分的情况下制作了表格,一切正常...现在表格根本不生成。
我知道我生成表单的方式不正确,但我不确定为什么......请帮助!
谢谢
function populateInventory() {
clearTable(); //This works
var artistIndex = byId('artist').selectedIndex;
var albumIndex = byId('albumSelect').selectedIndex;
var inventoryArray = inventoryNames[artistIndex][albumIndex];
for (i = 0; i < inventoryArray.length; i++) {
var idValue = inventoryArray[i][2];
var conditionValue = inventoryArray[i][3];
var priceValue = inventoryArray[i][4];
var table = byId('table');
var row = table.insertRow(i + 1);
var submitCell = row.insertCell(0);
var idCell = row.insertCell(1);
var conditionCell = row.insertCell(2);
var priceCell = row.insertCell(3);
//Begin problem?!
var form = document.createElement("form");
form.method = "post";
form.action = "<?php echo $_SERVER[PHP_SELF];?>";
var inventoryIdElement = document.createElement("<input name='inventoryIdElement' type='hidden' value='" + idValue + "' ></input>");
form.appendChild(inventoryIdElement);
var submitElement = document.createElement("<input name='submit' type='submit' value='Remove' ></input>");
form.appendChild(submitElement);
//End problem?!
var idElement = document.createTextNode(idValue);
var conditionElement = document.createTextNode(conditionValue);
var priceElement = document.createTextNode("$" + priceValue);
submitCell.appendChild(form);
idCell.appendChild(idElement);
conditionCell.appendChild(conditionElement);
priceCell.appendChild(priceElement);
}
}
Hey all
Trying to generate a 1-button form that will go in a cell of a table. The catch is that this needs to be generated dynamically in javascript. The table has a couple other values. I did the table first without the form part and everything was working fine... Now the table doesn't generate at all.
I know the way I'm generating the form is incorrect but I'm not sure why.... Please help!
Thanks
function populateInventory() {
clearTable(); //This works
var artistIndex = byId('artist').selectedIndex;
var albumIndex = byId('albumSelect').selectedIndex;
var inventoryArray = inventoryNames[artistIndex][albumIndex];
for (i = 0; i < inventoryArray.length; i++) {
var idValue = inventoryArray[i][2];
var conditionValue = inventoryArray[i][3];
var priceValue = inventoryArray[i][4];
var table = byId('table');
var row = table.insertRow(i + 1);
var submitCell = row.insertCell(0);
var idCell = row.insertCell(1);
var conditionCell = row.insertCell(2);
var priceCell = row.insertCell(3);
//Begin problem?!
var form = document.createElement("form");
form.method = "post";
form.action = "<?php echo $_SERVER[PHP_SELF];?>";
var inventoryIdElement = document.createElement("<input name='inventoryIdElement' type='hidden' value='" + idValue + "' ></input>");
form.appendChild(inventoryIdElement);
var submitElement = document.createElement("<input name='submit' type='submit' value='Remove' ></input>");
form.appendChild(submitElement);
//End problem?!
var idElement = document.createTextNode(idValue);
var conditionElement = document.createTextNode(conditionValue);
var priceElement = document.createTextNode("$" + priceValue);
submitCell.appendChild(form);
idCell.appendChild(idElement);
conditionCell.appendChild(conditionElement);
priceCell.appendChild(priceElement);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用一个好的库来进行 DOM 操作,例如 jQuery、YUI、Prototype、Dojo、Ext 等?对于这类事情,它会让你的生活更更轻松。
Have you considered using a good library for DOM manipulation, like jQuery, YUI, Prototype, Dojo, Ext, etc.? It would make your life much easier for this sort of things.
我认为您没有正确使用
createElement
。它只需要标签名称,而不需要整个标记。I don't think you are using
createElement
properly there. It takes just the tag name, not the whole markup.