每次单击都向表中添加行,从而操纵名称字段
adding rows to table per click, manipulating name field thereby
我找到了这个脚本,它在表中生成一行
此外,我还希望它更改其生成的行的名称字段,因为以后需要将每个新生成的行的数据保存到数据库中
但是,单击只会创建一行,而不会重命名该行的名称字段。
我已经把所有必要的代码片段都剪切到这些括号中。它应该是可复制粘贴的工作。
echo '<html><head>';
echo '<title>Adding rows</title>';
//the Java-script part which generates the new rows
//(i guess the "row-name"-manipulation must be embedded here, i just don't get how :-( )
echo
'<SCRIPT language="javascript">
function addRowEntry(tableID){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}
</SCRIPT>';
echo '</head>';
//the body-part which contains the button with the "onclick"-call on that function
echo '<body>';
//the form which on submit should pass all those created rows into the $_POST-Variables
echo '<form action="passToEvaluationScript.php" method="POST">';
//the button with the "onClick"-call
echo "<input type='button' value='add row entry' onClick=addRowEntry('myTable')>";
//the table which get the additional row
echo '<table id="myTable">';
echo '<tr>';
echo '<td>';
//this element should actually be renamed each click
//e.g. 10 consecutive clicks should create 10 of these "drop-down-boxes"
//each named country1, country2, ........ country10 respectively
echo '<SELECT name="country">
<OPTION value="in">India</OPTION>
<OPTION value="de">Germany</OPTION>
<OPTION value="fr">France</OPTION>
<OPTION value="us">United States</OPTION>
<OPTION value="ch">Switzerland</OPTION>
</SELECT>';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<input type="submit">';
echo '</form>';
echo '</body>';
echo '</html>';
这个元素甚至可以与页面上的其他<select>
元素一起使用:
<SCRIPT language="javascript">
function addRowEntry(tableID){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
// create a row element
var row = document.createElement("tr");
// add the row to the table
table.appendChild(row);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
// get the select element
var dropdown = row.getElementsByTagName("select")[0];
// get the current total of dropdowns in the table
var total = table.getElementsByTagName("select").length;
// set the name
dropdown.setAttribute("name", "country" + total);
}
</SCRIPT>
如果你想在新行上添加一个新的select元素,它的值来自上一个select,你可以尝试这样的方法:
<SCRIPT language="javascript">
function addRowEntry(tableID){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
document.getElementsByTagName('select')[document.getElementsByTagName('select').length - 1].setAttribute('name','country'+document.getElementsByTagName('select').length);
}
</SCRIPT>
如果这不是你想要的,请告诉我。感谢
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 在wordpress一定时间后更改自定义字段
- 如何在HTML输入字段中添加不可删除的后缀
- Meteor-添加用户自定义字段的方法不起作用
- 互斥单选按钮和相应的输入字段
- 在IE9中的输入字段中输入焦点最近按钮
- 选中单选框时将属性添加到输入字段
- 带有验证和隐藏字段值的提交按钮
- 主干窗体隐藏字段未呈现
- 带有let的JS/EECMAScript6私有字段的模式
- 按钮字段确认点击不'不起作用
- jQuery Wan Spinner插件的多个字段
- 通过单击动态加载的表中同一行的另一个字段来更新一行的字段
- 清除以前的$_GET值或不获取仅隐藏字段的值
- 每次单击都向表中添加行,从而操纵名称字段