例1:在编辑已有内容时通过ajax来对原有内容进行回显,input以及select
$(".changeShip").click(function(){
$("#get-shipType option").removeAttr("selected"); 在每次点击时首先对option的属性进行消除,以免 出现多个selected
var id = $(this).attr("date-exid"); //便获取对应的id
$.ajax({
type : "POST", //提交方式
url : "<%=request.getContextPath()%>/ship/show/edit/"+id, //传递ID进行查询
cache:false, //不对数据进行缓存
dataType:"json",
success : function(data)
{
$("#get-shipType option[value="+data.shipType+"]").attr("selected","selected"); //对比option的value来给对应的option添加属性
$("#get-id").val(data.id); //回显Input内的内容
$("#get-shipNumber").val(data.shipNumber);
$("#get-shipName").val(data.shipName);
}
})
});
例2:通过ajax查询后台内容并动态添加元素:
$(".changeShip").click(function()
{
$("#get-shipId").empty(); //每次点击都消除原先创建的元素 以免多次创建
$.ajax({
type : "GET", //提交方式
url : "<%=request.getContextPath()%>/shipBoat/list/shipNumber",
cache:false,
success : function(data)
{
if(data!=null) //如果查询内容非空
{
$.each(data,function(ind(索引值 0开始),dd(查询返回的对象))
{
$("#get-shipId").append("<option value ='"+dd.id+"'>"+dd.shipName+"</option>");//调用返回 对象内的内容进行创建
});
}else
{
//TODO
}
}
})