本文属于原创,转载请标明出处!

  近期在做项目时,要实现通过双击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变成可编辑,并保存到数据库中的更多相关文章

  1. 把Execl表格中的数据获取出来保存到数据库中

    比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中 ...

  2. 使用pandas中的raad_html函数爬取TOP500超级计算机表格数据并保存到csv文件和mysql数据库中

    参考链接:https://www.makcyun.top/web_scraping_withpython2.html #!/usr/bin/env python # -*- coding: utf-8 ...

  3. 把Dev的excel表格用clientdataset保存到数据库中。

    网上很多,如何把图片.word.excel等保存到数据库中.可是自己就是死活出现异常,百思不得其解.原因找到了,为什么没有去弄明白: 在sql server字段类型中,我把存储字段设成binary,结 ...

  4. 在jquery的ajax中添加自定义的header信息

    转自网络 1 $.ajax({ type: "POST", url: "http://192.168.0.88/action.cgi?ActionID=WEB_Reque ...

  5. 前端 HTML body标签相关内容 常用标签 表格标签 table

    表格标签 table 表格由<table> 标签来定义.每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由<td>标签定义).字母 td 指表 ...

  6. 使用Jquery的Ajax调用

    最近在学习web开发,试用了一下Jquery的ajax调用. 首先,新建一个MVC4的项目,在HomeController.cs中添加一个Action,命名为GetData, 通过这个为ajax提供数 ...

  7. 12 Servlet_04 Servlet增删改查 静态页面与动态页面 EL表达式 table表格的一些样式

    今天学习了servlet的增删改查: 存储数据 setAttribute(String name,Object obj );获取数据 getAttribute(String name);删除数据 re ...

  8. 数据库中table 和 schema的区别

    什么是Database,什么是Schema,什么是Table,什么是列,什么是行,什么是User? 我们可以把Database看作是一个大仓库,仓库分了很多很多的房间,Schema就是其中的房间,一个 ...

  9. jquery通过AJAX从后台获取信息并显示在表格上的类

    前一阵我写了:<jquery通过AJAX从后台获取信息并显示在表格上,并支持行选中.>现在,我把他们处理了一下,不需要每次写代码了: 具体代码如下: //获取数据并显示数据表格 funct ...

随机推荐

  1. Linux程序设计笔记

    使用size命令查看二进制文件时,结果并不一定和预测占用内存大小一致,因为可能存在内存对齐,导致内存字节数比实际的更多 在C语言中,字符串常量存放在text segment中,在C++中却是存储在da ...

  2. jq 事件冒泡总结

    什么是JS事件冒泡? 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个 ...

  3. Css3 javascript 写的分类

    不兼容IE10以下的浏览器 <!DOCTYPE html> <html> <head> <meta charset=utf-> <title> ...

  4. 使用Jstl异常:The absolute uri: http://java.sun.com/jsp/jstl/core cannot&nbs

    错误提示是:        org.apache.jasper.JasperException: This absolute uri http://java.sun.com/jsp/jstl/core ...

  5. window.onscroll

    http://www.w3help.org/zh-cn/causes/SD9013 1.各浏览器对 document.document.body.document.documentElement 对象 ...

  6. javascript 误用this指针 的情况

    理解了this指针后,我们再来看看一些很容易误用this指针的情况. 示例1——内联式绑定Dom元素的事件处理函数 <script type="text/javascript" ...

  7. use "man rsyslogd" for details. To run rsyslog interactively, use "rsyslogd -n"to run it in debug mo

    zjtest7-frontend:/root# service rsyslog start Starting system logger: usage: rsyslogd [options] use ...

  8. 【李婶小教程】(SE_lab3)

    额,今天说一下这个SE_lab3那几个东西都是怎么装的. 啊--其实技术含量貌似不是很高的. 1.先说最简单的一个叫做:Findbugs 点这个Eclipse Marketplace,这是一个神奇的东 ...

  9. 解决 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

    "Accept-Encoding": "gzip, deflate", 这条信息代表本地可以接收压缩格式的数据,而服务器在处理时就将大文件压缩再发回客户端,IE ...

  10. 你需要知道的九大排序算法【Python实现】之快速排序

    五.快速排序 基本思想:  通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分关键字小,则分别对这两部分继续进行排序,直到整个序列有序. 算法实现: ​ #coding: ...