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值呢?

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐