Jquery 动态生成表单 并将表单数据 批量通过Ajax插入到数据库
利用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;
}
}
Jquery 动态生成表单 并将表单数据 批量通过Ajax插入到数据库的更多相关文章
- v-if案例解析(element-ui form-item 结合 v-if 动态生成rule规则\表单元素,表单无法验证问题剖析 )
fire 读在最前面: 1.此文章衔接Vue 虚拟Dom 及 部分生命周期初探,相关整体知识点请先阅读后再继续本文阅读 问:当v-if为true时,会重新渲染相关dom节点吗? <child v ...
- BootStrap 智能表单系列 九 表单图片上传的支持
本章介绍如何在生成表单后,可以支持上传图片后可以及时预览图片 代码如下(连接地址:https://github.com/xiexingen/Bootstrap-SmartForm/blob/maste ...
- BootStrap 智能表单系列 六 表单数据绑定(编辑页面的数据绑定)
本章介绍如何在生成表单后,将一个model的数据展示到form表单中(一般用于编辑页面) 代码如下(连接地址:https://github.com/xiexingen/Bootstrap-SmartF ...
- BootStrap 智能表单系列 四 表单布局介绍
表单的布局分为自动布局和自定义布局两种: 自动布局就是根据配置项中第二级配置项中数组的长度来自动使用不同的bootstrap栅格,通过设置autoLayout为true可以实现自动布局 自动以布局就是 ...
- Rails-Treasure chest1 (自定义Model网址;多语言包; 时区设置, TimeZone类; 格式日期时间; 表单单选UI; 表单多选UI;Select2 Plugin)
自定义Model网址: 随机值网址SecureRandom.base58 多语言包, 包括默认语言设置和user自设置. 时区设置, TimeZone类 ,增加user自选时区功能 格式日期时间: x ...
- HTML 5 服务器发送事件、Input 类型、表单元素、表单属性
HTML5 服务器发送事件(server-sent event)允许网页获得来自服务器的更新. Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获取来自服务器 ...
- bootstrap 基础表单 内联表单 横向表单
bootstrap 基础表单 内联表单 横向表单 <!DOCTYPE html> <html> <head> <title></title> ...
- JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体
1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ...
- Html5学习进阶四 表单元素和表单属性
HTML5 的新的表单元素: HTML5 拥有若干涉及表单的元素和属性. 本章介绍以下新的表单元素: datalist keygen output 浏览器支持 Input type IE Firefo ...
随机推荐
- 【腾讯优测干货分享】如何降低App的待机内存(五)——优化dex相关内存及本章总结
本文来自于腾讯优测公众号(wxutest),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/01Abwe0p1h3WLh28Tzg_Dw 1.5案例:优化dex相 ...
- Javascript 基础知识笔记
标签(空格分隔): 廖老师学习笔记 javascript 基本入门 根据廖雪峰老师官网,自己看后的简单笔记 第一小节 基本知识 <script type="text/javascrip ...
- Oracle数据库编程:PL/SQL编程基础
2.PL/SQL编程基础: PL/SQL块: declare 定义部分 begin 执行部分 exception 异 ...
- windows 文件名太长无法删除的解决方法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Edit显示行号
Edit显示代码行号 关键点 使用这个类然后关联Edit的变量为 LineNumberEdit类型的 实现过程 //////////////////////////////////////////// ...
- AWS SQS DOC AND RUBY DEMO
# Amazon SQS 搜集整理aws sqs 的文档以及使用Ruby demo ## Amazon Simple Queue Service (SQS) 是一个可伸缩且可靠的消息传递框架,能够使用 ...
- Effective C# Chapter1-Language Elements
<EffectiveC#>这本书讲了一些关于C#语言的使用技巧和经验. 该系列文章是备忘录和自己的一些见解.程序猿们最喜欢这类问题了,欢迎讨论~ 菜单 Item 1 使用属性取代公共成员变 ...
- mysql监控管理工具--innotop
https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/innotop/innotop-1.9. ...
- c语言学习
#include <stdio.h>void sum_diff(int *n1,int *n2,int *sum,int *diff){ *sum=*n1+*n2; *diff=(*n1& ...
- 转:ORACLE制造方法的比较
转自:http://blog.itpub.net/133041/viewspace-438549/ 1.离散制造. 2.重复制造 3.流式制造 Oracle Applications 支持离散.项目. ...