菜单

Combobox 下拉框赋值

2018年11月19日 - Ajax

一般来说所示:

 1  string sql = "select distinct RoleName from tb_Role";
 2             DataTable dt = SqlHelper.DataTable(sql);
 3             cmb_Authority.Items.Clear();//移除下拉框中所有集合
 4             foreach (DataRow dr in dt.Rows)//遍历数据表中行的数据
 5             {
 6                 cmb_Authority.Items.Add(dr[0]);//把表中第一列的数据添加到下拉框中
 7             }
 8             if (xiu==0)
 9             {               
10                 cmb_Authority.SelectedIndex = 0;
11             }
12             if (xiu==1)
13             {
14                 btn_Reg.Text = "修改";
15                 txt_OperatorName.Text = tb_users.OperatorName;
16                 txt_UserName.Text = tb_users.UserName;
17                 txt_UserName.ReadOnly = true;
18                 txt_Pwd.Text = tb_users.Pwd;
19                 cmb_Authority.Items.Clear();//移除下拉框中所有集合
20                 cmb_Authority.Items.Add(tb_users.RoleName);
21                 cmb_Authority.SelectedIndex = 0;
22                 foreach (DataRow dr in dt.Rows)//遍历数据表中行的数据
23                 {
24                     if (dr[0].ToString()== tb_users.RoleName)
25                     {
26                         continue;
27                     }
28                     cmb_Authority.Items.Add(dr[0]);//把表中第一列的数据添加到下拉框中
29                 }
30 
31             }

 1 string sql = "select * from tb_Role"; //查询数据所有信息
 2             DataTable dt = SqlHelper.DataTable(sql);
 3             cmb_Authority.DataSource = dt; //下拉框数据源为dt
 4             cmb_Authority.DisplayMember = "RoleName";//显示成员
 5             cmb_Authority.ValueMember = "RoleID"; //值成员(类似与索引)
 6 
 7             //cmb_Authority.Items.Clear();//移除下拉框中所有集合
 8             //foreach (DataRow dr in dt.Rows)//遍历数据表中行的数据
 9             //{
10             //    cmb_Authority.Items.Add(dr[0]);//把表中第一列的数据添加到下拉框中
11             //}
12             //if (xiu==0)
13             //{               
14             //    cmb_Authority.SelectedIndex = 0;
15             //}
16             if (xiu==1)
17             {
18                 btn_Reg.Text = "修改";
19                 txt_OperatorName.Text = tb_users.OperatorName;
20                 txt_UserName.Text = tb_users.UserName;
21                 txt_UserName.ReadOnly = true;
22                 txt_Pwd.Text = tb_users.Pwd;
23                 cmb_Authority.SelectedValue = tb_users.RoleID; //(下拉框的值=这个索引)
24             }
<!DOCTYPE html>
<html>
<head>
<title>排课设置</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<link rel="stylesheet" href="/vendor/layui/css/layui.css" rel="external nofollow" media="all">
</head>
<body>
<div class="layui-form" lay-filter="boardFilter">
<hr>
<div class="layui-form-pane layui-tab-content" style="margin: -10px auto;">
<div class="layui-inline">
<label class="layui-form-label" style="width: 100px;">查询内容</label>
<div class="layui-input-inline" style="width: 250px;">
<input class="layui-input" name="schName" placeholder="请输入学校名称" id="schName" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label" style="width: 100px;">学期</label>
 <div class="layui-input-inline" style="width: 200px;">
<select name="termId" id="termId">

</select>
 </div>
</div>
<div class="layui-inline">
<label class="layui-form-label" style="width: 100px;">列表状态</label>
 <div class="layui-input-inline" style="width: 150px;">
<select name="msgType" id="msgType">
<option value="">全部状态</option>
<option value="1">申请中</option>
 <option value="2">确认申请</option>
 <option value="3">已经发布</option>
 <option value="0">已撤销</option>
</select>
 </div>
</div>
<div class="layui-inline toolbarBtn">
<button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit="" lay-filter="getSearch" id="getSearch" name="getSearch"><i class="layui-icon"></i> 查询</button>
</div>
</div>
</div>
<table class="layui-table" id="infoTable" lay-filter="filterTable"></table>
<script type="text/javascript" src="/vendor/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/vendor/layui/layui.js"></script>
<script type="text/html" id="action">
<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="view">查看</a>
{{# if(d.status===2){ }}
<a class="layui-btn layui-btn-xs" lay-event="send">推送</a>
{{# } }}
{{# if(d.status===3){ }}
 <a href='{{d.url}}' class='layui-btn layui-btn-xs'>下载排课表</a>
{{# } }}
</script>
<script type="text/html" id="courseArrangeFlag">
{{# if(d.courseArrangeFlag===0){ }}
 <h>行政班排课</h>
 {{# } else { }}
 <h>走班排课</h>
{{# } }}
</script>
<script type="text/html" id="status">
{{# if(d.status===0){ }}
 <h>撤消申请</h>
 {{# } else if(d.status===1){ }}
 <h>申请中</h>
{{# } else if(d.status===2){ }}
 <h>排课中</h>
{{# } else if(d.status===3){ }}
 <h>已经推送</h>
{{# } else if(d.status===4){ }}
 <h>已发布课程表</h>
{{# } }}
</script>
<script>
layui.use(['table','layer'], function() {
var table = layui.table
,form = layui.form;
var $ = layui.jquery, layer = layui.layer;

var tableIns = table.render({
elem: '#infoTable'
,url: '/web/courseArrange/list'
,id: 'idTable'
,page: true
,limits: [10,20,50,100]
,limit: 10 //默认采用10
,cols: [[ //标题栏
 {checkbox: true, fixed: true}
 ,{field: 'courseArrangeId', title: '序号', fixed: true}
 ,{field: 'schName', title: '学校名称'}
 ,{field: 'courseArrangeFlag', title: '排课类型', templet: '#courseArrangeFlag'}
 ,{field: 'termName', title: '学期'}
 ,{field: 'recordTime', title: '申请时间'}
 ,{field: 'status', title: '状态', templet: '#status'}
 ,{field: 'action', title: '操作', templet: '#action'}
 ]]
});

//监听管理操作
 form.on('switch(schControlInput)', function(obj){
 var normalValue
 if(obj.elem.checked){
 normalValue = '0';
 }else{
 normalValue = '1';
 }
$.ajax({
url:'/school/editIsNormalSchool',
type:"POST",
dataType:"json",
data:{"schId":this.value, "statusValue":normalValue},
success:function(res){
location.reload();
},
});
 });

// 监听Table工具条
table.on('tool(filterTable)', function(obj) {
var data = obj.data;
if (obj.event === 'view') {
location.href="courseArrangeView.html?id=" rel="external nofollow" +data.courseArrangeId+"&schId="+data.schId+"&areaId="+data.areaId;
}else if (obj.event === 'send') {
layer.open({
  type: 2,
  title: '导入并推送',
  maxmin: true,
  shadeClose: false, //点击遮罩关闭层
  area : ['700px' , '600px'],
  content: '/page/business/course/courseArrangeUpload.html?schId='+data.schId+'&areaId='+data.areaId+'&termId='+data.termId+'&courseArrangeFlag='+data.courseArrangeFlag+'&phone='+data.phone
  ,btn: '关闭'
  ,btnAlign: 'c' //按钮居中
  ,shade: 0 //不显示遮罩
  ,yes: function(){
   layer.closeAll();
   }
,end: function () {
    location.reload();
   }
});
}
});

form.on('submit(getSearch)', function(data){
 table.reload('idTable', {
where: data.field
});
});

$(document).ready(function () {
$("select[name=termNm]").empty();
 $.ajax({
  async: false,
  type: "POST",
  url: "/web/courseArrange/termList",
  dataType: "json",
  data: {},
  success: function (res) {
  var opt = "<option value='' selected = \'selected\'>全部</option>";
  $("select[name=termId]").append(opt);
for(var i=0; i<res.data.length; i++){
var option="<option value=\""+res.data[i].termId+"\"";
option += ">"+res.data[i].termName+"</option>"; //动态添加数据
$("select[name=termId]").append(option);
}
    layui.form.render('select','boardFilter');
  }
 });
});

});

</script>
</body>
</html>

一个是书库绑定 一个凡是重新循环赋值

上述这首layui
Ajax请求被下拉框赋值的实例就是稍稍编分享给大家的全部内容了,希望会于大家一个参阅,也意在大家多支持脚本的小。

乃可能感兴趣的文章:

相关文章

标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图