Home > Web Front-end > JS Tutorial > body text

Use Jquery to implement editable tables and use AJAX to submit to the server to modify the data_jquery

WBOY
Release: 2016-05-16 18:38:03
Original
1107 people have browsed it
The following is the js Code:
Copy the code The code is as follows:

$ (function() { // Equivalent to adding the onload event to the body tag in the page
$(".caname").click(function() { // Adding the click function to the tag with caname class in the page
var objTD = $(this);
var oldText = $.trim(objTD.text()); // Save the old category name
var input = $(""); // HTML code of the text box
objTD.html(input); // The content of the current td becomes the text box
// Set the text box The click event is invalid
input.click(function() {
return false;
});
// Set the style of the text box
input.css("border-width", "0px"); //The border is 0
input.height(objTD.height()); //The height of the text box is the height of the current td cell
input.width(objTD.width()) ; // The width is the width of the current td cell
input.css("font-size", "14px"); // The content text size of the text box is 14px
input.css("text-align ", "center"); // Center the text
input.trigger("focus").trigger("select"); // Select all
// When the text box loses focus, it changes back to text
input.blur(function() {
var newText = $(this).val(); // Modified name
var input_blur = $(this);
// As the old category The data submission operation is only performed when the name is different from the modified name
if (oldText != newText) {
// Get the ID (serial number) corresponding to the category name
var caid = $.trim (objTD.prev().text());
// AJAX asynchronously changes the database
var url = "../handler/ChangeCaName.ashx?caname=" encodeURI(encodeURI(newText)) "&caid= " caid "&t=" new Date().getTime();
$.get(url, function(data) {
if (data == "false") {
$("#test ").text("Category modification failed, please check whether the category name is duplicated!");
input_blur.trigger("focus").trigger("select"); // Select all text boxes
} else {
$("#test").text("");
objTD.html(newText);
}
});
} else {
// before and after The text is the same, turn the text box into a label
objTD.html(newText);
}
});
// Press a keyboard key in the text box
input.keydown( function(event) {
var jianzhi = event.keyCode;
var input_keydown = $(this);
switch (jianzhi) {
case 13: // Press the Enter key to change The resulting value is submitted to the database
// $("#test").text("The key value you pressed is: " jianzhi);
var newText = input_keydown.val(); // After modification The name of Serial number)
var caid = $.trim(objTD.prev().text());
// AJAX asynchronously changes the database
var url = "../handler/ChangeCaName.ashx?caname= " encodeURI(encodeURI(newText)) "&caid=" caid "&t=" new Date().getTime();
$.get(url, function(data) {
if (data == "false ") {
$("#test").text("Category modification failed, please check whether the category name is duplicated!");
input_keydown.trigger("focus").trigger("select"); // Select all text boxes
} else {
$("#test").text("");
objTD.html(newText);
}
});
} else {
// Make the text before and after the same, turn the text box into a label
objTD.html(newText);
}
break;
case 27: // Press Esc key to cancel the modification and change the text box into a label
$("#test").text("");
objTD.html(oldText);
break;
}
});
});
});

// Shield Enter key
$(document).keydown(function(event) {
switch (event.keyCode) {
case 13: return false;
}
});



Here is the general handler code ChangeCaName.ashx


Copy code The code is as follows: <%@ WebHandler Language="C#" Class="ChangeCaName" %>
using System;
using System.Web;
using BLL;
using Model;
public class ChangeCaName : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string caid = context.Request.QueryString["caid"];
string caname = context.Server.UrlDecode(context.Request. QueryString["caname"]);
// Determine whether the category with the same name already exists in the database
if (new CategoryManager().IsExists(caname))
{
context.Response.Write(" false");
return;
}
// Change database category name
Category ca = new Category(caid, caname);
bool b = new CategoryManager().Update(ca );
if (b)
{
context.Response.Write("true");
}
else
{
context.Response.Write("false ");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template