JQuery结合Ajax实现双击Table表格,使Table变成可编辑,并保存到数据库中
本文属于原创,转载请标明出处!
近期在做项目时,要实现通过双击Table表格的TR,使Table行变成可编辑,来实现修改数据并保存到数据库中的功能,无需多说,直接贴代码吧。希望能得到各位同仁指正。
function tdEdit(element, id) {
var i_a = "<input class='edit_td' type='text' style='height:30px; width:40px;' value='";
var i_b = "'/>";
var t_a = "<textarea class='tarea' cols='63' rows='3' style='width:90%'>";
var t_b = "</textarea>";
var td = $(element).find("td");
if (td.length > 0) {
var sc = $(element).children().eq(1).text();
var ss = $(element).children().eq(2).text();
var sequence = $(element).children().eq(3).text();
var weight = $(element).children().eq(4).text();
var max = $(element).children().eq(5).text();
var min = $(element).children().eq(6).text();
var cv = $(element).children().eq(7).text();
var explain = $(element).children().eq(8).text();
$(element).children().eq(1).html($(t_a + sc + t_b));
$(element).children().eq(2).html($(t_a + ss + t_b));
$(element).children().eq(3).html($(i_a + sequence + "'id='num1" + i_b));
$(element).children().eq(4).html($(i_a + weight + "'id='num2" + i_b));
$(element).children().eq(5).html($(i_a + max + "'id='maxvalue" + i_b));
$(element).children().eq(6).html($(i_a + min + "'id='minvalue" + i_b));
$(element).children().eq(7).html($(t_a + cv + t_b));
$(element).children().eq(8).html($(t_a + explain + t_b));
}
$(".edit_td").click(function () {
return false;
});
$(".tarea").click(function () {
return false;
});
//获取焦点
$(".edit_td").trigger("focus");
$(".tarea").trigger("focus");
//文本框失去焦点后提交内容,重新变为文本
$(".save").click(function () {
//验证信息"n":/^\d+$/
var reg = /^[0-9]+\.{0,1}[0-9]{0,2}$/;
var num1 = $("#num1").val();
var num2 = $("#num2").val();
var max = $("#maxvalue").val();
var min = $("#minvalue").val();
if (parseInt(min) > parseInt(max)) {
alert("最小值不能大于最大值!");
return false;
}
if (!reg.test(num1) || !reg.test(num2) || !reg.test(max) || !reg.test(min)) {
alert("请输入数字!");
return false;
}
//重新赋上新值
$(".edit_td").each(function (i) {
var newtxt = $(this).val();
$(element).children().eq(i + 3).html(newtxt);
});
$(".tarea").each(function (j) {
var newtarea = $(this).val();
if (j < 2) {
$(element).children().eq(j + 1).html(newtarea);
}
else {
$(element).children().eq(j + 5).html(newtarea);
}
});
var new_sc = $(element).children().eq(1).text();
var new_ss = $(element).children().eq(2).text();
var new_sequence = $(element).children().eq(3).text();
var new_weight = $(element).children().eq(4).text();
var new_max = $(element).children().eq(5).text();
var new_min = $(element).children().eq(6).text();
var new_cv = $(element).children().eq(7).text();
var new_explain = $(element).children().eq(8).text();
if (new_sc != sc || new_ss != ss || new_sequence != sequence || new_weight != weight || new_max != max || new_min != min || new_cv != cv || new_explain != explain) {
$.ajax({
type: 'POST',
contentType: 'application/json',
url: '/Ajax/AjaxAction.ashx/UpdateRuleDetail',
data: '{id:"' + id + '",strCon:"' + new_sc + '",strStandard:"' + new_ss + '",Sequence:"' + new_sequence + '",Weight:"' + new_weight + '",CandidateValue:"'
+ new_cv + '",MaxValue:"' + new_max + '",MinValue:"' + new_min + '",Explain:"' + new_explain + '"}',
dataType: 'json',
async: true,
beforeSend: function () {
},
success: function (data) {
alert("保存成功!");
window.location.reload(); //刷新页面
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + ':' + textStatus);
window.location.reload();
}
});
}
else {
alert("温馨提示:您没有做任何修改!");
window.location.reload();
}
});
}
JS
前台页面绑定:
<tr ondblclick="tdEdit(this,@item.ID)"></tr>
Html
最终效果图:

JQuery结合Ajax实现双击Table表格,使Table变成可编辑,并保存到数据库中的更多相关文章
- 把Execl表格中的数据获取出来保存到数据库中
比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中 ...
- 使用pandas中的raad_html函数爬取TOP500超级计算机表格数据并保存到csv文件和mysql数据库中
参考链接:https://www.makcyun.top/web_scraping_withpython2.html #!/usr/bin/env python # -*- coding: utf-8 ...
- 把Dev的excel表格用clientdataset保存到数据库中。
网上很多,如何把图片.word.excel等保存到数据库中.可是自己就是死活出现异常,百思不得其解.原因找到了,为什么没有去弄明白: 在sql server字段类型中,我把存储字段设成binary,结 ...
- 在jquery的ajax中添加自定义的header信息
转自网络 1 $.ajax({ type: "POST", url: "http://192.168.0.88/action.cgi?ActionID=WEB_Reque ...
- 前端 HTML body标签相关内容 常用标签 表格标签 table
表格标签 table 表格由<table> 标签来定义.每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由<td>标签定义).字母 td 指表 ...
- 使用Jquery的Ajax调用
最近在学习web开发,试用了一下Jquery的ajax调用. 首先,新建一个MVC4的项目,在HomeController.cs中添加一个Action,命名为GetData, 通过这个为ajax提供数 ...
- 12 Servlet_04 Servlet增删改查 静态页面与动态页面 EL表达式 table表格的一些样式
今天学习了servlet的增删改查: 存储数据 setAttribute(String name,Object obj );获取数据 getAttribute(String name);删除数据 re ...
- 数据库中table 和 schema的区别
什么是Database,什么是Schema,什么是Table,什么是列,什么是行,什么是User? 我们可以把Database看作是一个大仓库,仓库分了很多很多的房间,Schema就是其中的房间,一个 ...
- jquery通过AJAX从后台获取信息并显示在表格上的类
前一阵我写了:<jquery通过AJAX从后台获取信息并显示在表格上,并支持行选中.>现在,我把他们处理了一下,不需要每次写代码了: 具体代码如下: //获取数据并显示数据表格 funct ...
随机推荐
- linux笔记2.20
用户相关: /etc/passwd 用户信息 /etc/shadow 密码信息 /etc/group 组信息 添加用户: useradd -u -g 修改用户: usermod - ...
- NGINX关于配置PATHINFO
最近在群里发现有很多小白不会配置pathinfo现贴出来配置代码照着配置就可以了 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...
- delphi2010 开发及调试WebService 实例
使用delphi已经10多年了,一直搞桌面程序开发,对Webservice一直很陌生,近来因工作需要,学习delphi开发WebService,担心遗忘,作此笔记. 特别感谢 中塑在线技术总监 大犇 ...
- C++ AO读取shapefile的属性值
C++ AO读取一个shapefile文件的所有属性值 #include "stdafx.h" #include "iostream.h" #inc ...
- Array and its point.
a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...
- 存储过程中update,然后用sql%判断update是否成功的存储过程
--更新用户状态 PROCEDURE P_UPDATE_USER_STATUS ( v_SrcID IN NUMERIC, v_DstID IN NUMERIC, v_DstType IN NUMER ...
- 【HDOJ】2577 How to Type
DP. /* 2577 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...
- CPU天梯图
一幅图,了解CPU
- vijos国庆节模拟赛之繁星春水
A.闪烁的繁星 题目:https://vijos.org/p/1881 题解:貌似做过小白逛公园或者序列操作都可以秒出吧,就是pushup函数比较麻烦,不过仔细想一想就知道了. 代码: #includ ...
- HDU_2136——最大质因数,素数筛选法
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...