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 ...
随机推荐
- strust1.x中formbean的原理及作用
from: http://blog.csdn.net/tuiroger/article/details/3947896 今天张老师讲了一些比较重要的strust标签,<html:link ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- VisualBox会造成VPN连接不上问题
今天连接VPN查找点资料,但是怎么也连接不上,一直出现651错误,开始以为是服务器配置原因,重新配置后发现问题依旧,后查找相关资料,发现有提到VisualPC, 感觉VisualBox跟VisualP ...
- ehcache简单使用
项目中需要实现一个功能,定时查询FTP服务器某个目录下的文件,并及时下载至本机,同时不能消耗太多系统资源. 最后实现是使用ehcache,将文件路径和文件大小缓存,如果前后两次无变化,则忽略.如果同一 ...
- [三]JFreeChart实践二
功能: 1.设置带色彩的柱状图 2.可以设置多组数据的展示 3.可以设置图标的背景色 4.可以设置柱与柱之间的距离 5.可以设置柱子上边是否显示具体的数值
- oracle中使用sql查询时字段为空则赋值默认
转至:http://www.th7.cn/db/Oracle/201501/86125.shtml oracle 通过 nvl( )函数sql 查询时为 空值 赋默认值 oracle 函数介绍之nvl ...
- Windows上常见的集中布尔类型的比较
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Windows上常见的集中布尔类型的比较.
- Openstack中间DVR Part1 -- 东西走向的交通处理
作者:Liping Mao 发表于:2014-07-04 版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 在Openstack中L3router会造成流量集中 ...
- celery expires 让celery任务具有时效性
起因:有的时候.我们希望任务具有时效性.比方定时每5分钟去抓取某个状态,由于celery队列中的任务可能非常多,等到这个任务被运行时.已经超过了5分钟,那么这个任务的运行已经没有意义.由于下一次抓取已 ...
- jsp----在jsp中写java代码(变量和函数方法)
<%@page import="java.text.SimpleDateFormat"%><%@page language="java" im ...