我有以下页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Open Inn Management Site</title>
<link href="http://www.openjs.com/scripts/ui/calendar/calendar.css" type="text/css" rel="stylesheet" />
<link type="text/css" rel="stylesheet" href="css/jquery.flexbox.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" />
<script type="text/javascript" src="js/jquery.flexbox.min.js" />
<script type="text/javascript">
$.ready(function() {
alert("welcome");
$('#fb').flexbox('json.jsp');
});
</script>
<SCRIPT LANGUAGE="JavaScript" SRC="http://www.openjs.com/scripts/ui/calendar/calendar.js"></SCRIPT>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/1.css" type="text/css" media="screen,projection" />
</head>
<body>
<div id="fb">
<input type="hidden" id="fb_hidden" name="fb" value="">
<input id="fb_input" autocomplete="off" class="fb-input watermark" style="width: 183px; ">
<span id="fb_arrow" class="fb-arrow out"></span>
<div id="fb_ctr" style="width: 202px; top: 22px; left: 0px; display: none; " class="fb">
<div class="content"></div>
<div></div>
</div>
</div>
</body>
</html>
和以下 json.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%@ page contentType="application/json" %>
<%@ page pageEncoding="UTF-8" %>
<%
int i=1;
String s="test";
String a="{\"results\":[{\"id\":"+i+",\"name\":\""+s+"\"}]}";
System.out.println(a);
%>
</body>
</html>
css 和 js 的所有目录都是正确的...问题是为什么它现在显示一个组合框?我在这里做错了什么?我认为 ready 函数没有被调用,因为警报没有出现
最佳答案
存在三个问题:
你需要
$(document).ready()
而不是$.ready()
(如许多 jQuery 教程中所述)。<script type="text/javascript"> $(document).ready(function() { alert("welcome"); $('#fb').flexbox('json.jsp'); }); </script>
否则它将在文档准备好加载时被调用,当元素带有
id="fb"
时。可能还没有。您在单个响应中混合了 HTML 和 JSON。摆脱 HTML 困惑。 HTML 不是 JSON。
您只将 JSON 写入系统输出(最终出现在标准输出和/或服务器日志文件中),而不是响应主体。替换
System.out.println()
通过out.print()
.完整的 JSP 应如下所示:<%@ page contentType="application/json" pageEncoding="UTF-8" %><% int i = 1; String s = "test"; String a = "{\"results\":[{\"id\":" + i + ",\"name\":\"" + s + "\"}]}"; out.print(a); %>
(确保
%>
和<%
之间没有换行!)实际上,JSP 是这项工作的错误位置。使用 Servlet。
关于javascript - JSP 页面中的 FlexBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295634/