我在這裏的問題是,當我嘗試添加行的功能似乎不工作按鈕「添加行」沒有做任何事情。我試圖捕獲值,並給他們的名字像item1 [] price1 [] ... 我需要捕獲和調用的價值與PHP因爲我是非常新的JavaScript我不能真正寫我的代碼只在jscript。動態輸入不工作
function addRow(tableID,ID1,ID2,ID3) {
var table = document.getElementById(tableID);
var table1 = document.getElementById(ID1).id;
var table2 = document.getElementById(ID2).id;
var table3 = document.getElementById(ID3).id;
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
\t \t var x = document.getElementById(tableID).id;
\t \t
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name="chkbox[]";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = "<input type='text' name="+table1+" id="+table1+"/>";
var cell3 = row.insertCell(2);
cell3.innerHTML = "<input type='text' name="+table2+" id="+table2+"/>";
var cell4 = row.insertCell(3);
cell4.innerHTML = "<input type='text' name="+table3+" id="+table3+"/>";
\t \t }
function deleteRow(tableID) {
try { \t
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="Author" content="" />
<meta name="Keywords" content="Programmation,Web,PHP,MySQL,MariaDB" />
<meta name="Description" content="" />
\t <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body> \t
<HEAD>
\t <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
\t <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
\t <SCRIPT language="javascript">
<?php
$groups = 5; //number of groups
$rows = 3; //number of rows per group
$arr = array('Monday','Tuesday','Wednesday','Thursday','Friday');
$arr1 = array(
array(1,2,3),
array(4,5,6),
array(7,8,9),
array(10,11,12),
array(13,14,15)
);
$arr2= array('soup','maindish','dessert');
\t for($g = 0; $g < $groups; ++$g)
{
\t \t \t
?>
\t \t \t <table id='<?php echo $g; ?>'>
\t \t \t <thead>
<tr> \t \t
<caption><?php echo $arr[$g]; ?></caption>
</tr>
<th></th>
<th></th>
<th width="94">Item</th>
<th width="121">Price</th>
<th width="84">Qty</th>
</thead>
<?php
for($r=0; $r < $rows; ++$r)
{
\t \t \t \t ?>
<form action='tablebuilder.php' method='post'>
<tbody id="datatable<?php echo $arr1[$g][$r]; ?>">
<tr>
\t <th rowspan="1000"><?php echo $arr2[$r];?></th> \t
\t <td><input type='checkbox'></td>
\t <td><input type='text' name='item<?php echo $arr1[$g][$r];?>[]' id='item<?php echo $arr1[$g][$r];?>[]'></td>
\t <td><input type='text' name='price<?php echo $arr1[$g][$r]; ?>[]' id='price<?php echo $arr1[$g][$r]; ?>[]'></td>
\t <td><input type='text' name='qty<?php echo $arr1[$g][$r]; ?>[]' id='qty<?php echo $arr1[$g][$r]; ?>[]'></td>
\t <td><input type="button" value="AddRow" Onclick="addRow('datatable<?php echo $arr1[$g][$r]; ?>','item<?php echo $arr1[$g][$r];?>[]','price<?php echo $arr1[$g][$r]; ?>[]','qty<?php echo $arr1[$g][$r]; ?>[]')"/></td>
\t <td><input type="button" value="DeleteRow" Onclick="deleteRow('datatable<?php echo $arr1[$g][$r];?>')"/></td>
</tr>
</tbody>
\t
<?php
\t \t \t }
}
?>
</table>
\t <input type='submit' name='submit' value='submit'/>
\t \t </form>
<?php \t \t
if (isset($_POST['submit'])){
echo $_POST['qty1'][1];
} \t
?>
</body>
</html> \t
我使用這裏的HTML/PHP代碼是創建5和表3行每行分別是湯,主菜和甜點。
*「按鈕‘添加一行’就是什麼也不做*」你有沒有進行錯誤檢查您的瀏覽器控制檯試圖運行你的'javascript'功能(S)? – NewToJS
它不工作,因爲JohnDoe添加了.id –
歡迎來到Stack Overflow!請[參觀],環顧四周,並仔細閱讀[幫助],尤其是[*我如何提出一個好問題?*](/幫助/如何問)請更新您的問題**可運行** [mcve]使用Stack Snippets('[<>]'工具欄按鈕)顯示問題。您創建的代碼段不可運行。請注意編輯器UI中框中的標籤以及側面的按鈕,並確保在單擊** Run code snippet **時複製問題。 –