AJAX和select,绑定值,动态加载select的Item值,设置所选的值 (全都是客户端控件)

  //根据年级获取班级 

dataType: 'json':在一班处理程序中获取数据时,JSON为string类型
  function btnGetBJ(njm,bh) {
  $.ajax({
 cache: false,
  type: "GET",
 url: 'TPJG.ashx?HandlerType=getbj&njm=' + njm + '&xqh=1', dataType: 'json',
 success: function (json) {
 //AJAX加载select if (json != "" || json != null || json != undefined) {
 document.getElementById("seletBJ").innerHTML = "";
 document.getElementById('seletBJ').add(new Option("---"""));
 for (var i = 0; i < json.length; i++) {
 document.getElementById('seletBJ').add(new Option(json[i].texts, json
 
 [i].values));
 }
 //如果报对象不支持此属性或方法用此方法
                   $("#BJ option").remove();
                    $("#BJ").prepend("<option value=''>---</option>")
                    if (json != "" && json != null && json != undefined) {
                        for (var i = 0; i < json.length; i++) {
                            $("#BJ").prepend("<option value='" + json[i].BH + "'>" + json[i].BJMC + "</option>");
                        }
                    }
 

 

//设置选中项 if (bh != "") {var all_options = document.getElementById("seletBJ").options;
for (var j = 0; j < all_options.length; j++) {
 if (all_options[j].value == bh) {
 all_options[j].selected = true;
 }
 }
 }
 }
 }
});
 }

 

//页面初始化时,加载select的选择项 

$(function () { btnGetBJ('<%= njm %>''<%= bh %>');
 })
 
 
 
 
 
 //后台绑定select,设置选择项
 <option value="">---</option>
 <% foreach (var dic in xqList)
{ %><option value="<%= dic.XQH %>" <%= Request.QueryString["seletXQ"] == 
 
dic.XQH ?"selected":""%>>
<%= dic.XQMC%></option>
<% } %>
</select>

 

posted @ 2015-04-15 12:00  Tz__C  阅读(444)  评论(0编辑  收藏  举报