关于jquery显示图片的问题,图片地址从数据库查出来,返回前台,显示不了图片,代码如下:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%Stringbp=request.ge...
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String bp = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String path = request.getContextPath();
String jlbh = "";
jlbh = request.getParameter("jlbh");
%>
<script type="text/javascript">
var jlbh ='<%=jlbh%>';
var imph = '<%=path%>';
var bp = '<%=bp%>' ;
var c = bp + basePath;
alert(c);
var params = {jlbh:jlbh};
$jquery.ajax({
type:"POST",
sync:true,
url:contextPath+"/sm/cp/img/selectImg.action",
data:params,
dataType:"json",
success:function(json){
if(json.length>=1){
for(var i=0;i<json.length;i++){
var pathImg =json[i].imgpath;
//var src = bp+"uploadImg/"+pathImg;
var src = bp+basePath+"/uploadImg/"+pathImg;
//$jquery("#show").append(src);
alert("<img src=\""+ src +"\" />");
//$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
}
}
}
});
</script>
</head>
<body>
<center>
<div id ='udDiv'>
<div id="preview">
<img id="imghead" width='200px' height='200px' border=0 src=''/>
</div>
<br/>
<form id='imgForm' enctype="multipart/form-data" method="post">
<input type="file" name="fileName" id="upfile" onchange="previewImage(this)" />
<br/>
<input type="text" id ="jlbh" name ="jlbh" value="<%=jlbh %>" style="display:none"//>
<br/>
<input type="submit" id='tijiao' value="上传图片" style="display:none"/>
<a class="easyui-linkbutton" iconCls="icon-ok"
onclick='submitClick()'>上传</a>
<a class="easyui-linkbutton" iconCls="icon-cancel"
onclick='window.close()'
style="margin-left: 20px; margin-rigth: 50px">关闭</a>
</form>
</div>
<div id ='show' style='display:block'>
</div>
</center>
</body>
</html> 展开
<%
String bp = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String path = request.getContextPath();
String jlbh = "";
jlbh = request.getParameter("jlbh");
%>
<script type="text/javascript">
var jlbh ='<%=jlbh%>';
var imph = '<%=path%>';
var bp = '<%=bp%>' ;
var c = bp + basePath;
alert(c);
var params = {jlbh:jlbh};
$jquery.ajax({
type:"POST",
sync:true,
url:contextPath+"/sm/cp/img/selectImg.action",
data:params,
dataType:"json",
success:function(json){
if(json.length>=1){
for(var i=0;i<json.length;i++){
var pathImg =json[i].imgpath;
//var src = bp+"uploadImg/"+pathImg;
var src = bp+basePath+"/uploadImg/"+pathImg;
//$jquery("#show").append(src);
alert("<img src=\""+ src +"\" />");
//$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
}
}
}
});
</script>
</head>
<body>
<center>
<div id ='udDiv'>
<div id="preview">
<img id="imghead" width='200px' height='200px' border=0 src=''/>
</div>
<br/>
<form id='imgForm' enctype="multipart/form-data" method="post">
<input type="file" name="fileName" id="upfile" onchange="previewImage(this)" />
<br/>
<input type="text" id ="jlbh" name ="jlbh" value="<%=jlbh %>" style="display:none"//>
<br/>
<input type="submit" id='tijiao' value="上传图片" style="display:none"/>
<a class="easyui-linkbutton" iconCls="icon-ok"
onclick='submitClick()'>上传</a>
<a class="easyui-linkbutton" iconCls="icon-cancel"
onclick='window.close()'
style="margin-left: 20px; margin-rigth: 50px">关闭</a>
</form>
</div>
<div id ='show' style='display:block'>
</div>
</center>
</body>
</html> 展开
2个回答
展开全部
把javascript部分放到文档底部吧,像上面这样肯定不行的,因为javascript执行的时候,DOM文档还没准备好。如果真要把javascript放头部的话,就放在$(document).ready()里也可以的,就像这样的了:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String bp = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String path = request.getContextPath();
String jlbh = "";
jlbh = request.getParameter("jlbh");
%>
<script type="text/javascript">
$(document).ready(function(){
var jlbh ='<%=jlbh%>';
var imph = '<%=path%>';
var bp = '<%=bp%>' ;
var c = bp + basePath;
alert(c);
var params = {jlbh:jlbh};
$jquery.ajax({
type:"POST",
sync:true,
url:contextPath+"/sm/cp/img/selectImg.action",
data:params,
dataType:"json",
success:function(json){
if(json.length>=1){
for(var i=0;i<json.length;i++){
var pathImg =json[i].imgpath;
//var src = bp+"uploadImg/"+pathImg;
var src = bp+basePath+"/uploadImg/"+pathImg;
//$jquery("#show").append(src);
alert("<img src=\""+ src +"\" />");
//$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
}
}
}
});
});
</script>
</head>
<body>
<center>
<div id ='udDiv'>
<div id="preview">
<img id="imghead" width='200px' height='200px' border=0 src=''/>
</div>
<br/>
<form id='imgForm' enctype="multipart/form-data" method="post">
<input type="file" name="fileName" id="upfile" onchange="previewImage(this)" />
<br/>
<input type="text" id ="jlbh" name ="jlbh" value="<%=jlbh %>" style="display:none"//>
<br/>
<input type="submit" id='tijiao' value="上传图片" style="display:none"/>
<a class="easyui-linkbutton" iconCls="icon-ok"
onclick='submitClick()'>上传</a>
<a class="easyui-linkbutton" iconCls="icon-cancel"
onclick='window.close()'
style="margin-left: 20px; margin-rigth: 50px">关闭</a>
</form>
</div>
<div id ='show' style='display:block'>
</div>
</center>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String bp = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String path = request.getContextPath();
String jlbh = "";
jlbh = request.getParameter("jlbh");
%>
<script type="text/javascript">
$(document).ready(function(){
var jlbh ='<%=jlbh%>';
var imph = '<%=path%>';
var bp = '<%=bp%>' ;
var c = bp + basePath;
alert(c);
var params = {jlbh:jlbh};
$jquery.ajax({
type:"POST",
sync:true,
url:contextPath+"/sm/cp/img/selectImg.action",
data:params,
dataType:"json",
success:function(json){
if(json.length>=1){
for(var i=0;i<json.length;i++){
var pathImg =json[i].imgpath;
//var src = bp+"uploadImg/"+pathImg;
var src = bp+basePath+"/uploadImg/"+pathImg;
//$jquery("#show").append(src);
alert("<img src=\""+ src +"\" />");
//$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
$jquery("#show").append("<img width='100px' height='100px' src=\""+ src +"\" />");
}
}
}
});
});
</script>
</head>
<body>
<center>
<div id ='udDiv'>
<div id="preview">
<img id="imghead" width='200px' height='200px' border=0 src=''/>
</div>
<br/>
<form id='imgForm' enctype="multipart/form-data" method="post">
<input type="file" name="fileName" id="upfile" onchange="previewImage(this)" />
<br/>
<input type="text" id ="jlbh" name ="jlbh" value="<%=jlbh %>" style="display:none"//>
<br/>
<input type="submit" id='tijiao' value="上传图片" style="display:none"/>
<a class="easyui-linkbutton" iconCls="icon-ok"
onclick='submitClick()'>上传</a>
<a class="easyui-linkbutton" iconCls="icon-cancel"
onclick='window.close()'
style="margin-left: 20px; margin-rigth: 50px">关闭</a>
</form>
</div>
<div id ='show' style='display:block'>
</div>
</center>
</body>
</html>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |