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 ...
随机推荐
- Thinkphp 连接数据库、查询、添加
一.连接数据库配置及Model数据模型层 1. Thinkphp\conf\convertion.php中找到数据库设置部分,复制到自己的配置文件中,并添加好有关数据库的内容 JiaoWu\Home\ ...
- 记一次 nginx 504 Gateway Time-out
今天程序在执行一项excel导出任务的时候 出现了nginx超时的提示 nginx 504 Gateway Time-out 排查过程: 查看该任务 发现内容是一个数据量20000条信息 每条信息有5 ...
- 命令passwd报错因inode节点处理记录
命令passwd报错因inode节点处理记录故障现象:1.修改密码时报错 passwd: Authentication token manipulation error2.添加用户报错:unable ...
- HTML5数据存储
介绍两种对象使用方法: sessionStorage方法如果关闭了浏览器,这个保存的数据就丢失. 1.sessionStorage 保存数据:sessionStorage.setItem(key,va ...
- Flask源码阅读笔记(一)
作者:acezio链接:https://zhuanlan.zhihu.com/p/21358368来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. flask的url r ...
- NVIDIA 显卡温度提示过高,显卡温度高99度怎么办?
你可能没有在意您的NVIDIA(英伟达)显卡温度,那么你就不会知道显卡温度过高如何导致的呢?也不会知道如果去解决显卡温度过高的问题了,在此我希望在电脑玩游戏或者在办公中电脑速度快慢,或者发热的情况下, ...
- Verilog HDL中阻塞语句和非阻塞语句的区别
在Verilog中有两种类型的赋值语句:阻塞赋值语句(“=”)和非阻塞赋值语句(“<=”).正确地使用这两种赋值语句对于Verilog的设计和仿真非常重要. Verilog语言中讲的阻塞赋值与非 ...
- Android监听事件
ListView事件监听: setOnItemSelectedListener 鼠标滚动时触发 setOnItemClickListener 点击时触发 EditText事件监听: setOnKeyL ...
- [JS]_proto_和prototype到底有啥区别
是时候拿出我珍藏多年的这张图了: 首先,要明确几个点: 1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的 ...
- 使用dom4j生成xml字符串,以及解析xml字符串
基于dom4j-1.6.1.jar import java.io.IOException; import java.io.StringWriter; import java.util.ArrayLis ...