Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在html表中添加行_Javascript_Html_Asp.net - Fatal编程技术网

Javascript在html表中添加行

Javascript在html表中添加行,javascript,html,asp.net,Javascript,Html,Asp.net,在下面的代码中,我有一个html表,我需要在其中添加行到html表并插入到数据库中。但我无法添加行。请帮助我纠正这个问题。我的目标是避免回发,所以我选择javascript添加行 <script language="javascript"> function addRow(tableID) { var table = document.getElementById("<%=dataTable.ClientID%>").value; var ro

在下面的代码中,我有一个html表,我需要在其中添加行到html表并插入到数据库中。但我无法添加行。请帮助我纠正这个问题。我的目标是避免回发,所以我选择javascript添加行

<script language="javascript">        
  function addRow(tableID) {
  var table = document.getElementById("<%=dataTable.ClientID%>").value;
  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;
      switch (newcell.childNodes[0].type) {
          case "text":
              newcell.childNodes[0].value = "";
              break;
          case "checkbox":
              newcell.childNodes[0].checked = false;
              break;
          case "select-one":
              newcell.childNodes[0].selectedIndex = 0;
              break;
      }
  }
}

function deleteRow(tableID) {
  try {
      var table = document.getElementById("<%=dataTable.ClientID%>");
      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) {
              if (rowCount <= 1) {
                  alert("Cannot delete all the rows.");
                  break;
              }
              table.deleteRow(i);
              rowCount--;
              i--;
          }
      }
  } catch (e) {
      alert(e);
  }
}
</script>



  <input type="button" value="Add Row" onclick="addRow('dataTable')"/>

    <input type="button" value="Delete Row" onclick="deleteRow('dataTable')"/>

    <table id="dataTable" width="350px" border="1" runat="server">
        <tr>
            <td><input type="checkbox" name="chk" runat="server"/></td>
            <td><input type="text" name="txt" runat="server"/></td>
            <td>
                 <asp:DropDownList ID="ddlProduct" runat="server" OnSelectedIndexChanged="ddlProduct_SelectedIndexChanged"  AutoPostBack="true" Style="width: 100%; height:23px" ></asp:DropDownList>
            </td>
        </tr>
    </table>

函数addRow(tableID){
var table=document.getElementById(“”)值;
var rowCount=table.rows.length;
var row=table.insertRow(rowCount);
var colCount=table.rows[0].cells.length;
对于(变量i=0;i如果(rowCount这些行是问题:

var table = document.getElementById("<%=dataTable.ClientID%>").value;
var row = table.insertRow(rowCount); 
var table=document.getElementById(“”).value;
var row=table.insertRow(rowCount);
请将其更正为:

var tableBody = document.getElementById("<%=dataTable.ClientID%>").getElementsByTagName("tbody")[0];
var row = tableBody .insertRow(rowCount); 
var tableBody=document.getElementById(“”).getElementsByTagName(“tbody”)[0];
var row=tableBody.insertRow(rowCount);
行插入到
表体
t表体
)和/或
表头
thead


var table=document.getElementById(“”).value
将返回
未定义的
。value属性仅用于输入以检索或设置输入的内容。

您可以提供
文档。getElementById
带有服务器生成的
id
,还可以将
tableID
传递给函数。这似乎是多余的。@Mouser我只需复制视图源代码并粘贴即可是的,如何使用C#在单击“保存”按钮时将值插入数据库不是您最初的问题的一部分。但是,要么将值包装成带有输入的表单,要么使用AJAX调用。将值发布到您的aspx文件中。如果我选择下拉值,表行将消失。我不知道自动回发值的作用是什么?可能它会刷新页