gpt4 book ai didi

javascript - 每次点击向表中添加行,从而操作名称字段

转载 作者:行者123 更新时间:2023-12-02 19:48:26 26 4
gpt4 key购买 nike

我发现这个脚本在表中生成一行。
我还希望它更改它生成行的名称字段,因为后面需要将新生成的每一行的数据保存到数据库中。
但是,单击仅创建一行,而不会重命名该行的名称字段。

<小时/>

我已将所有必要的代码片段剪切到这些括号中。它应该是可复制粘贴的。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com