关于用$.Ajax()控制按钮的选择与查看
Posted Azjs丶V1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于用$.Ajax()控制按钮的选择与查看相关的知识,希望对你有一定的参考价值。
前端jsp页面代码:
请选择版本:
<br/>
<div>
推送版本:<input type="button" id="post1" class = "ver" value="推送"/><br/>
响应版本:<input type="button" id="post2" class = "ver" value="响应"/><br/>
当前版本查看:<button id="post3" value="vercheck">查看</button>
</div>
<br/>
<br/>
<div id = "result"></div>
<script src="<%=path%>/js/jquery.min.js"></script>
<script src="<%=path%>/js/verselect.js"></script>
js控制代码:
$(function(){
$(‘#post1‘).click(function(){
//alert(1);
$(‘#result‘).innerhtml="";
var vers = "0";
$.ajax({
dataType: "html",
type: ‘POST‘,
url: g.basePath+‘/verSelect.do‘,
data: {
vers
},
success: function(data){
$(‘#result‘).html(data)
}
});
})
$(‘#post2‘).click(function(){
//alert(2);
$(‘#result‘).innerHTML="";
var vers = "1";
$.ajax({
dataType: "html",
type: ‘POST‘,
url: g.basePath+‘/verSelect.do‘,
data: {
vers
},
success: function(data){
$(‘#result‘).html(data)
}
});
})
$(‘#post3‘).click(function(){
//alert(3);
$(‘#result‘).innerHTML="";
var ver = $(‘#post3‘).val();
$.ajax({
dataType: "html",
type: ‘POST‘,
url: g.basePath+‘/verSelect.do‘,
data: {
ver
},
success: function(data){
$(‘#result‘).html(data)
}
});
})
})
后台请求处理,此处使用post提交请求
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class VerSelectServlet extends HttpServlet {
public static version = false;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String vers = request.getParameter("vers");
if ("1".equals(vers)) {
response.getWriter().print("已切换到当前版本");
version = ture;
}
if ("0".equals(vers)) {
response.getWriter().write("已切换到当前版本");
version = false;
}
String ver = request.getParameter("ver");
if ("vercheck".equals(ver)) {
if (!version) {
response.getWriter().write("当前为0版本");
}
if (version) {
response.getWriter().write("当前为1版本");
}
}
}
}
以上是关于关于用$.Ajax()控制按钮的选择与查看的主要内容,如果未能解决你的问题,请参考以下文章