package com.imooc.ajax;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
/**
* Servlet implementation class SongServlet
*/
@WebServlet("/SongServlet")
public class SongServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SongServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List songList=new ArrayList();
Song s1=new Song("稻香","千千阙歌","一块红布");
Song s2=new Song("晴天","傻女","假行僧");
Song s3=new Song("告白气球","七友","新长征路上的摇滚");
songList.add(s1);
songList.add(s2);
songList.add(s3);
String json=JSON.toJSONString(songList);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println(json);
}
}html>
Insert title here$("input").click(function(){
$("#content").text("");
$.ajax({
"url" : "/ajax/SongServlet",
"type" : "get",
// "data" : {}
"dataType" : "json" ,
"success" :function(json){
console.log(json);
for(var i=0;i
var input_id=$("input").attr("id");
if(input_id=="but1"){
$("#content").append("
"+json[i].lxSong+"
");}else if(input_id=="but2"){
$("#content").append("
"+json[i].jdSong+"
");}else if(input_id=="but3"){
$("#content").append("
"+json[i].ygSong+"
");}
}
},
"error" :function(xmlhttp , errorText){
console.log(xmlhttp);
console.log(errorText);
if(xmlhttp.status=="405"){
alert("无效的请求方式");
}else if(xmlhttp.status=="404"){
alert("未找到URL资源");
}else if(xmlhttp.status == "500"){
alert("服务器内部错误,请联系管理员");
}else{
alert("产生异常,请联系管理员");
}
}
})
})
请问老师,有没有办法获取到当前点击的input按钮的id值呢?