一、功能介绍

完全依靠jQuery,表单table新增行(按键盘tab增一行,按esc删一行),也可以加一个新增行按钮点击新增多行,这里就不多说了~~

二、该功能主要实现技术:

1,总体.NET MVC

2,前台:bootstrap,jQuery

3,后台:EF框架。

三、代码展示

1、HTML代码:

@model MSModel.Menus
<table id="tbCate" class="table invoice-table">
<thead>
<tr>
<th></th>
<th>类型</th>
<th>菜名</th>
<th>价格</th>
<th>单位</th>
<th>备注</th>
<th></th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.Menu.Count; i++)
{
<tr>
<td>1</td>
<td>
@*@Html.DropDownList("selCateType0", ViewBag.MenuList as IEnumerable<SelectListItem>)*@ @*, new { @name = "selCateType" }*@
<select id="selCateType0"></select>
@Html.TextBoxFor(c => c.Menu[i].Type, new { @type = "hidden" })
</td>
<td>
@Html.TextBoxFor(c => c.Menu[i].Name, new { @class = "form-control" })
</td>
<td>
@Html.TextBoxFor(c => c.Menu[i].Price, new { @class = "form-control", @type = "number" })
</td>
<td>@Html.TextBoxFor(c => c.Menu[i].Unit, new { @class = "form-control" })</td>
<td width="226">
@Html.TextAreaFor(c => c.Menu[i].Remark, new { @class = "form-control", @rows = "1", @cols = "16" })
</td>
<td><a href="javascript:void(0)" class="navbar-link glyphicon glyphicon-remove"></a></td>
</tr>
}
</tbody>
</table>

2、JS代码:

<script type="text/javascript">
var keydwonc;
var temptr;
var temp; $(function () {
temptr = $("#tbCate tbody").first().html(); //复制行 下面新增行用到
temp = $("#tbCate tbody").html();//保存渲染前的模板
     //easyUI下拉绑定
$("#tbCate select").combobox({
width: 210, height: 34, editable: true,
url: '/Cates/GetCateListbyType',
valueField: 'Id', textField: 'Type',
mode: 'remote', hasDownArrow: false,//是否显示下拉箭头
onBeforeLoad: function (param) {
if (param == null || param.q == null || param.q.replace(/ /g, '') == '') {
var value = $(this).combobox('getValue');
if (value) {// 修改的时候才会出现q为空而value不为空
param.id = value;
return true;
}
return false;
}
}
});
$("#tbCate a").remove();//删除第一个删除按钮
$('#Menu_0__Price').val("");
     //==============此处给第二行(新增行)赋的事件================
keydwonc = function (et) {
       //按键盘tab新增一行
if (event.keyCode == 9) {
//$(et).unbind();//取消当前事件
var rowNo = $("#tbCate tbody tr").size();
var index = Number($("#tbCate tbody tr td").find('textarea').last().attr("name").match(/\d+/)[0]);
$(temptr).appendTo($("#tbCate tbody")).find("a").click(function () {
$(this).parent().parent().remove();//删除整行
}).parent().parent().find("select").last().combobox({
width: 210, height: 34, editable: true,
url: '/Cates/GetCateListbyType',
valueField: 'Id', textField: 'Type',
mode: 'remote', hasDownArrow: false,//是否显示下拉箭头
onBeforeLoad: function (param) {
if (param == null || param.q == null || param.q.replace(/ /g, '') == '') {
var value = $(this).combobox('getValue');
if (value) {// 修改的时候才会出现q为空而value不为空
param.id = value;
return true;
}
return false;
}
}
});//加载新行并渲染
$(et).parent().parent().nextAll().last().find("td:first").html(rowNo + 1);//新增行自增编号
//新行 属性改变
$(et).parent().parent().nextAll().last().find("input,select,textarea").each(function (i) {
$(this).val("");
//combobox 属性改变
var attid = $(this).attr("id");
var attname = $(this).attr("name");
var attcomboname = $(this).attr("comboname");
var newindex = index + 1;
if (attid)
$(this).attr("id", attid.replace(/\d+/, newindex));
if (attname)
$(this).attr("name", attname.replace(/\d+/, newindex));
if (attcomboname)
$(this).attr("comboname", attcomboname.replace(/\d+/, newindex)); if ($(this).attr("name") == "Menu[" + newindex + "].Remark") { $(this).attr("onkeydown", "keydwonc(this)");
}
})
} else if (event.keyCode == 27) { //按esc删一行
if ($('#tbCate tbody tr').size() > 1) {
$(et).parents('tr').remove();
//光标返回聚焦
$('#tbCate tbody tr').find('textarea').last().focus();
}
}
};
     //================此处第一行事件==========================
var tr = $("#tbCate tbody tr");
tr.each(function () {
$(this).find('textarea').last().keydown(function (e) {
if (e.keyCode == 9) {
var rowNo = $("#tbCate tbody tr").size(); var index = Number($("#tbCate tbody tr td").find('textarea').last().attr("name").match(/\d+/)[0]);
//新增行,开始
$(temp).appendTo($("#tbCate tbody")).find("a").click(function () {
$(this).parent().parent().remove();//删除整行
}).parent().parent().find("select").combobox({
width: 210, height: 34, editable: true,
url: '/Cates/GetCateListbyType',
valueField: 'Id', textField: 'Type',
mode: 'remote', hasDownArrow: false,//是否显示下拉箭头
onBeforeLoad: function (param) {
if (param == null || param.q == null || param.q.replace(/ /g, '') == '') {
var value = $(this).combobox('getValue');
if (value) {// 修改的时候才会出现q为空而value不为空
param.id = value;
return true;
}
return false;
}
}
});//加载新行并渲染
$(this).parent().parent().nextAll().last().find("td:first").html(rowNo + 1);//新增行自增编号
//新增行 最后一行属性改变
$(this).parent().parent().nextAll().last().find("input,select,textarea").each(function (i) {
$(this).val("");
//combobox 属性改变
var attid = $(this).attr("id");
var attname = $(this).attr("name");
var attcomboname = $(this).attr("comboname");
var newindex = index + 1;
if (attid)
$(this).attr("id", attid.replace(/\d+/, newindex));
if (attname)
$(this).attr("name", attname.replace(/\d+/, newindex));
if (attcomboname)
$(this).attr("comboname", attcomboname.replace(/\d+/, newindex));
if ($(this).attr("name") == "Menu[" + newindex + "].Remark") {
$(this).attr("onkeydown", "keydwonc(this)");
}
})
}
});
});
});
</script>

四、效果图:

五、总结

本人写的jQuery按tab键新增行这套逻辑代码算是中规中矩的,可以说不是很简洁的,肯定还有地方要优化,欢迎有看到的园友来指正,哈哈。

能与大家共同学习进步是我最大的兴趣!!

基于jQuery表单快速录入数据功能的更多相关文章

  1. jquery表单提交获取数据(带toast dialog)

    最近写了一个召集令,传统表单提交注册.写写遇到的费时间的点与解决办法 git项目地址:form-demo(针对于手机版,懒人可以直接使用,有排版和样式) demo使用Jquery,toast使用jqu ...

  2. 表单快速启用城市地区功能 齐博x1齐博x2齐博x3齐博x4齐博x5齐博x6齐博x7齐博x8齐博x9齐博x10

    比如分类系统\application\fenlei\config.php 修改这个文件,里边加入参数 'use_area'=>true, 那么会员中心与后台的,修改发布页面,都会自动加上城市地区 ...

  3. django中ModelForm save方法 以及快速生成空表单或包含数据的表单 包含错误信息

    django中ModelForm学习系列一~save方法 Model代码 from django.db import models # Create your models here. class P ...

  4. SpreadJS V13.0发布,聚焦表单设计与数据交互,让您的工作效率突飞猛进!

    纯前端表格控件SpreadJS,是一款成功应用于华为.招商银行.天弘基金.苏宁易购等国内外知名企业的前端开发工具,其带来的价值不仅体现在帮助开发人员在其Web应用程序中快速构建 Web Excel . ...

  5. 循序渐进VUE+Element 前端应用开发(27)--- 数据表的动态表单设计和数据存储

    在我们一些系统里面,有时候会需要一些让用户自定义的数据信息,一般这些可以使用扩展JSON进行存储,不过每个业务表的显示项目可能不一样,因此需要根据不同的表单进行设计,然后进行对应的数据存储.本篇随笔结 ...

  6. django 基于form表单上传文件和基于ajax上传文件

    一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...

  7. jQuery——表单异步提交

    如果不做任何处理,表单提交时会刷新页面,为了改善体验,可以使用jQuery做到异步提交表单:通过$("#form").serialize()将表单元素的数据转化为字符串,然后通过$ ...

  8. jQuery框架学习第十一天:实战jQuery表单验证及jQuery自动完成提示插件

    jQuery框架学习第一天:开始认识jQueryjQuery框架学习第二天:jQuery中万能的选择器jQuery框架学习第三天:如何管理jQuery包装集 jQuery框架学习第四天:使用jQuer ...

  9. JQuery -- Validate, Jquery 表单校验

    1. Jquery 表单验证需要插件 jQuery validation 1.7  ---验证插件需要:jQuery 1.3.2 或 1.4.2版本 http://jquery.bassistance ...

随机推荐

  1. Go执行远程ssh命令

    使用包:golang.org/x/crypto/ssh 以下封装一个发送命令的Cli结构体 type Cli struct { IP string //IP地址 Username string //用 ...

  2. 【转】Tableau 9.3.8 desktop for Mac 中文破解

    tableau破解版本下载地址 安装步骤: 1. 编辑hosts 文件 在终端输入:sudo nano /etc/hosts 添加如下内容: 127.0.0.1 licensing.tableauso ...

  3. Maven代理设置

    公司需要设置代理才能上网,而运行Maven时需要下载依赖的库. 怎么办呢? 原来Maven也像IE一样,可以设置HTTP代理的. 步骤如下: ·编辑 ~/.m2/setting.xml 文件.如果该目 ...

  4. centos6.5 redis搭建

    redis安装1.wget http://download.redis.io/redis-stable.tar.gz2.tar -zxvf redis-stable.tar.gz3.cd redis- ...

  5. 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2023  Solved: 778 ...

  6. 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 10059  Solved: 4863[Submit ...

  7. PE解析器的编写(三)——区块表的解析

    PE文件中所有节的属性都被定义在节表中,节表由一系列的IMAGE_SECTION_HEADER结构排列而成,每个结构用来描述一个节,结构的排列顺序和它们描述的节在文件中的排列顺序是一致的. 具有相同属 ...

  8. ResourceBundleViewResolver

    1 springmvc中ResourceBundleViewResolver解析器的使用1.1 springmvc.xml的配置因为我配置了多个解析器,所以额外的加了order属性,value值越低, ...

  9. 破解附近寝室的Wifi密码

    [系统]运行在VMware虚拟机中的Kali Linux系统.(实测Kali运行在virtualbox中兼容性很差,VMware支持的很好.我认为这正是一个不要迷信开源的例子,多数情况下,大公司的商业 ...

  10. Java与算法之(2) - 快速排序

    快速排序的基本思路是,每次选定数列中的一个基准数,将小于基准数的数字都放到基准数左边,大于基准数的数字都放到基准数右边.然后再分别对基准数左右的两个数列分别重复以上过程.仍以4 3 6 2 7 1 5 ...