2017-08-08 75 views
0

我在这里的问题是,当我尝试添加行的功能似乎不工作按钮“添加行”没有做任何事情。我试图捕获值,并给他们的名字像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行每行分别是汤,主菜和甜点。

+0

*“按钮‘添加一行’就是什么也不做*”你有没有进行错误检查您的浏览器控制台试图运行你的'javascript'功能(S)? – NewToJS

+0

它不工作,因为JohnDoe添加了.id –

+0

欢迎来到Stack Overflow!请[参观],环顾四周,并仔细阅读[帮助],尤其是[*我如何提出一个好问题?*](/帮助/如何问)请更新您的问题**可运行** [mcve]使用Stack Snippets('[<>]'工具栏按钮)显示问题。您创建的代码段不可运行。请注意编辑器UI中框中的标签以及侧面的按钮,并确保在单击** Run code snippet **时复制问题。 –

回答

1

从以下代码中删除.id代码。

table1 = document.getElementById(ID1).id; 
     var table2 = document.getElementById(ID2).id; 
     var table3 = document.getElementById(ID3).id; 
var x = document.getElementById(tableID).id; 

,并使其

table1 = document.getElementById(ID1); 
      var table2 = document.getElementById(ID2); 
      var table3 = document.getElementById(ID3); 
    var x = document.getElementById(tableID); 
+1

我试图删除.id已经和'添加行'按钮开始再次工作。但是,一旦我测试,看看我是否可以捕获我用echo $ _POST ['price1'] [1]创建的额外输入的任何值;什么都没发生。只有第一个值被捕获echo $ _POST ['price1'] [0]; – JohnDoe

+1

你能告诉我你的代码在tablebuilder.php吗? –

+0

我在代码片段中发布了完整的代码,我将尝试在主线程中对其进行编辑以使其运行。但idk为什么这段代码不读取代码的php部分... – JohnDoe