请有更多经验的人帮我解决这个问题吗?
这不起作用,我不知道为什么。
我是从互联网上得到的,但是示例是使用php,所以,不确定。
Update.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java"%>
<%@page import="java.lang.*" import="org.postgresql.*" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/postgres";
Connection connection = DriverManager.getConnection(url,
"postgres", "admin");
String strquery = "UPDATE projects SET project = '"
+ request.getParameter("id") + "'";
Statement st = connection.createStatement();
//System.out.println("Connecting to database...");
ResultSet rs = st.executeQuery(strquery);
System.out.println(strquery);
rs.close();
st.close();
} catch (Exception e) {
System.out.println(e);
}
%>
</body>
</html>
ContenteditableTest.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contenteditable test</title>
<script>
$('.id').keyup(function() {
delay(function() {
var text = $('.id').text();
$.ajax({
type : "post",
url : "update.jsp",
data : "text=" + text,
success : function(data) {
console.log('success bro!');
}
});
}, 500);
});
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
</script>
</head>
<body>
<table>
<tr>
<td contenteditable="true" class="id">1</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:1)
您在update.jsp中首先提到request.getParameter('id')
但是在ajax调用中的ContenteditableTest.jsp中您提到了data : "text=" + text
,因此将"text="
更改为"id="
在update.jsp中的ContenteditableTest.jsp或request.getParameter('id')
到request.getParameter('text')
。
对于contenteditable属性,您可以关注此帖子 https://plus.google.com/114254873085584811912/posts/LqpzvvzoMYc