您的位置:首页 > 产品设计 > UI/UE

获取easyui-combobox value值

2016-06-12 16:58 495 查看
背景:

    其实这是个常用的不能再常用的小功能。但当初自己也废了不少劲。现在发现有些人对这块还比较模糊,实现不了。所以在巩固知识的同时分享给大家。

功能描述:

    点击学院的combox,选择学院。然后根据学院ID查出该学院对应的课程。并加载到课程的combox中。



难点:

    获取combox的文本值,不难。我做的时候遇到的问题就是获取不到学院ID。从valueFiled中得到的也总是学院名称。

实现:

    我们在使用easyui-combobox时,一般会在textField存放名称,valueField存放ID。

    页面加载时查询学院信息:

View:

<span style="margin: 0 120px;">学院:
<select  class="easyui-combobox" id="college" name="college"
data-options="url:'/AddExam/QueryCollege',
method:'get',
valueField:'CollegeID',
textField:'CollegeName',
panelHeight:'auto',
onSelect:function(){
QueryCourseByCollege();
}
"
style="width: 150px; margin-right: 100px; font-size: 16px;">


Controller:假数据举例:

public ActionResult QueryCollege()
{
List<ExamManageViewModel> ExamManageViewModelList = new List<ExamManageViewModel>();

ExamManageViewModel enExamManageViewModel = new ExamManageViewModel();
enExamManageViewModel.CollegeID = "20121111";
enExamManageViewModel.CollegeName = "外国语学院";
ExamManageViewModel enExamManageViewModelnew = new ExamManageViewModel();
enExamManageViewModelnew.CollegeID = "20123333";
enExamManageViewModelnew.CollegeName = "物电学院";
//将实体添加到list中
ExamManageViewModelList.Add(enExamManageViewModel);
ExamManageViewModelList.Add(enExamManageViewModelnew);
return Json(ExamManageViewModelList, JsonRequestBehavior.AllowGet);

}


选择学院,触发JS查询课程方法:

function QueryCourseByCollege() {
//获取选择的学院ID
var CollegeID = $('#college').combobox('getValue');
if (CollegeID == null || CollegeID == "") {
alert("请先选择学院");
}
else {

$.post('/AddExam/QueryCourseByCollege?CollegeID=' + CollegeID, {}, function (data) {
var cmbSchoolYear = $('#course').combobox({
url: '/AddExam/QueryCourseByCollege?CollegeID=' + CollegeID,
editable: false,
method: 'get',
valueField: 'CourseID',
textField: 'CourseName',
});
});
}
}
Controller:查询课程

public ActionResult QueryCourseByCollege()
{
//获取前台传来的学院的ID
string CollegeID = Request["CollegeID"];

List<ExamManageViewModel> ExamManageViewModelList = new List<ExamManageViewModel>();

ExamManageViewModel enExamManageViewModel = new ExamManageViewModel();
//给实体赋值
enExamManageViewModel.CourseID = "20122222";
enExamManageViewModel.CourseName = "大学英语(一)";
//将实体添加到list中
ExamManageViewModelList.Add(enExamManageViewModel);
return Json(ExamManageViewModelList,JsonRequestBehavior.AllowGet);

注意点:

    我之前一直出问题:

    var CollegeID = $('#college').combobox('getValue');

    获取不到学院ID。

    原因是controller中返回的不是json串。

    所以,这是一点需要注意的地方。

另外:

    Controller赋值的时候:enExamManageViewModelnew.CollegeID = "20123333"与:view中获取值:valueField:'CollegeID'的实体名称一定要一致,如这里的CollegeID。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: