利用Jquery 動態產生 Table 表單 之後利用each 方法來遍曆所有文字框擷取文本的value值 並通過Ajax 將資料 提交到Web服務裡把資料插入資料庫Html頁面<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script src="js/Jquery1.7.js"></script> <script src="js/MyAdd.js"></script> <link href="css/MyPages.css" rel="stylesheet" /></head><body> <div > <div id="divInsert"> <div id="divBtn"> <input id="btnDelete" type="button" value="刪除行" /> <input id="btnData" type="button" value="插入行" /> <input id="btnInsert" type="button" value="添加行" /> </div> </div> <table id="tab" cellpadding="0" cellspacing="0"> <tr> <td rowspan="2">姓名</td> <td rowspan="2">年齡</td> <td colspan="2">血壓</td> </tr> <tr> <td>高壓</td> <td>低壓</td> </tr> </table> </div> <div id="mydiv"></div></body></html>JS文檔/// <reference path="../WebService1.asmx" />/// <reference path="../WebService1.asmx" />$(function () { //定義一個全域變數i,用來標識添加了幾行 var row = 0; var strValue = ""; //將一行添加到table中去 $('#btnInsert').click(function () { row++; //字串拼接tr一行中的內容 var tr = "<tr>"; for (var i = 0; i < 3; i++) { tr += "<td><input id='" + row + "text'" + i + "+' type='text' value=" + row +""+ i + " /></td>"; } tr += "<td><input class='txt' id='" + i + "text4'+ type='text' value=" + row + "" + 4 + " /><input id='Checkbox1' class='ck' name='ckb' type='checkbox' /> </td></tr>"; $("#tab").append(tr); }) //刪除添加的行,先判斷checkbox是否選中,然後刪除 $('#btnDelete').click(function () { $("input[name=ckb]:checked").each(function () { $(this).parent().parent().remove(); }); }) //將i遍曆,判斷是否存有值,如果有將資料插入資料庫 $('#btnData').click(function () { $('table input').each(function () { strValue += $(this).val() + ","; }) $.ajax({ type: 'post', contentType: 'application/json', url: "../WebService1.asmx/InsertInfo", data: "{valuesStr:'" + strValue + "'}", success: function (result) { $('#mydiv').html(result.d); } }) })}) CSS文檔table tr td{border:1px solid #eee;text-align:center;width:80px;}#divInsert{width:100%;height:25px;}#btnInsert{width:50px;height:25px;background-color:#eee;border-style:none;position:absolute;left:185px;}#btnData{width:50px;height:25px;background-color:#eee;border-style:none;position:absolute;left:235px;}#btnDelete{width:50px;height:25px;background-color:#eee;border-style:none;position:absolute;left:285px;}input{width:70px;}.txt{width:35px;float:left;position:relative;left:5px;}.ck{width:10px;float:right;} PersonInfo類檔案namespace ASPOilfiled{ public class PersonInfo { public string Name { get; set; } public int Age { get; set; } public int Hblood { get; set; } public int Lblood { get; set; } }}WebService1.asmx 檔案 [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { public static string sqlstr = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString; [WebMethod] public string InsertInfo(string valuesStr) { string result = "插入失敗!"; string asd = valuesStr.Replace("on,", ""); string[] str2 = System.Text.RegularExpressions.Regex.Split(asd, ","); //for (int i = 0; i < str2.Length; i++) //{ // result += str2[i] + "<br/>"; //} for (int i = 0; i < str2.Length; i++) { PersonInfo info; if (i % 4 == 0) { info = new PersonInfo(); info.Name = str2[i]; info.Age = Convert.ToInt32(str2[i + 1]); info.Hblood = Convert.ToInt32(str2[i + 2]); info.Lblood = Convert.ToInt32(str2[i + 3]); result = InfoToSql(info).ToString(); } } return result; } public int InfoToSql(PersonInfo info) { string instSql = "insert into PersonInfo values(@name,@age,@Hblood,@Lblood)"; SqlConnection con = new SqlConnection(sqlstr); con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = instSql; cmd.Parameters.Add("@name", SqlDbType.VarChar, 32).Value = info.Name; cmd.Parameters.Add("@age", SqlDbType.Int).Value = info.Age; cmd.Parameters.Add("@Hblood", SqlDbType.Int).Value = info.Hblood; cmd.Parameters.Add("@Lblood", SqlDbType.Int).Value = info.Lblood; int i = cmd.ExecuteNonQuery(); cmd.Dispose(); con.Dispose(); return i; } }