使用javascript将表单插入表中

时间:2014-08-07 06:56:33

标签: javascript html

我正在尝试将Form插入表中,但未插入<form>标记(仅输入类型)。

$(function () {
    var table = document.getElementById("transport");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    var cell5 = row.insertCell(4);
    var cell6 = row.insertCell(5);
    var cell7 = row.insertCell(6);
    var cell8 = row.insertCell(7);
    var cell9 = row.insertCell(8);
    var cell10 = row.insertCell(9);
    cell1.innerHTML = "<form action='/Transport/Grid' id='paieska' method='post'>";
    cell2.innerHTML = "<br><br><input class=\"form-control\" id=\"slicplate\" name=\"slicplate\" placeholder=\"Valst. Nr.\" type=\"text\" value=\"\">";
    cell10.innerHTML = "</form>";
});

2 个答案:

答案 0 :(得分:0)

正如我在评论中提到的那样。

  

form开始和结束标记应包含在容器中。

但是你是在一个单元格中开始并在另一个单元格中关闭。那不行。

相反,你可以这样做:

cell1.innerHTML = "<form action='/Transport/Grid' id='paieska' method='post'>";
cell1.innerHTML += "<br><br><input class=\"form-control\" id=\"slicplate\" name=\"slicplate\" placeholder=\"Valst. Nr.\" type=\"text\" value=\"\">";
cell1.innerHTML += "</form>";

所以你知道,有很多更好的方法......这是一种老式的方式..(在评论中再次提到)

答案 1 :(得分:0)

var data =[];
function add(){

var fname = document.getElementById("firstname").value,
      lname= document.getElementById("lastname").value,
         email = document.getElementById("email").value,
         age = document.getElementById("age").value,
         mobile = document.getElementById("mobile").value,
         gen = gender1.checked?gender1.value:gender2.value,

         date = document.getElementById("date").value,
         adress = document.getElementById("address").value,
         city = document.getElementById("city").value,
          state = document.getElementById("state").value,


          zip = document.getElementById("zip").value,
        pan = document.getElementById("pan").value,
         tan = document.getElementById("tan").value,
          aadhar = document.getElementById("aadhar").value,
           obj = {name1:fname,name2:lname,mail:email,aged:age,num:mobile,gend:gen,tarrek:date,
            add:adress,place:city,stat:state,zpicode:zip,panc:pan,tanc:tan,aadharc:aadhar}
            data.push(obj);

            var table =  "<table class='table table-striped table-bordered margin' id='delet'>";
            table+= "<tr><th>Firstname</th><th>Lastname</th><th>Email</th><th>Age</th><th>Mobile</th><th>Gender</th><th>Date</th><th>Address</th><th>City</th><th>State</th><th>Documents</th><th>Zip</th><th>Pan</th><th>Tan</th><th>Aadhar</th></tr>";
                for(var i=0;i<data.length;i++){
                    table+="<tr><td>"+data[i].name1+"</td><td>"+data[i].name2+"</td><td>"+data[i].mail+
                    "</td><td>"+data[i].aged+"</td><td>"+data[i].num+"</td><td>"+data[i].gend+
                    "</td><td>"+data[i].tarrek+"</td><td>"+data[i].add+"</td><td>"+data[i].place+
                    "</td><td>"+data[i].stat+"</td><td>"+data[i].zpicode+"</td><td>"+data[i].panc+
                    "</td><td>"+data[i].tanc+"</td><td>"+data[i].aadharc+
                    "</td></tr>"
                }
                table+="</table>";

                document.getElementById("table-data").innerHTML=table;

};