我发现此脚本在表中生成一行。
我还希望它更改它生成的行的名称字段,
因为每个新生成的行的数据都需要稍后保存到数据库中。
但是,单击只会创建一行,而不会重命名该行的名称字段。
我在这里将所有必要的代码片段都切入了这些括号中。它应该是可复制粘贴的。
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>
关于javascript - 每次点击将行添加到表中,从而操作名称字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9601883/