1.场景:从数据库获取分类数据,在下拉框中显示,
//获取所有分类显示出来
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/new/getCategoryName",
success:function(data){
console.log(data);
$.each(JSON.parse(data),function(i,retVal){
console.log(retVal.id);
/*使用JQ*/
/*$("#select").append("<option value='"+retVal.id+"'>"+retVal.name+"</option>"); */
/*使用JS*/
var sel= document.getElementById("select");
sel.options.add(new Option(retVal.name,retVal.id));
});
},
error: function(data){
console.log(data);
alert("分类查询错误");
}
});
注意:下拉框中选择之后,看不到selected属性,但是该option确实已经被选中。


