JavaScript
x
19
19
1
$('#DepartmentCode').change(function () {
2
var empName = $('#Employee_No');
3
var department = $('#DepartmentCode').val();
4
empName.empty();
5
6
$.ajax({
7
url: "/LeaveRequestEntry/GetEmployeesByDept",
8
type: "POST",
9
contentType: "application/json; charset=utf-8",
10
dataType: "json",
11
data: JSON.stringify({ ClassCode: department }),
12
success: function (data) {
13
$.each(data, function (index, option) {
14
empName.append('<option value=' + option.Value + '>' + option.Text + '</option>');
15
});
16
}
17
});
18
});
19
This is my JavaScript code.
JavaScript
1
3
1
ViewBag.Employee = employeeRepository.GetAllByDepartment(ClassCode).Select(e => new SelectListItem { Text = e.Full_Name + "_" + e.No, Value = e.No });
2
return Json(new SelectList(ViewBag.Employee, "Value", "Text"));
3
This is my controller part.
JavaScript
1
7
1
<div class="field">
2
@Html.DropDownList("Department", new SelectList(ViewBag.DepartmentList, "Value", "Text"), LeaveRequestEntryRes.SelectableMessage, new { @class = "ui fluid dropdown", @id = "DepartmentCode" })
3
</div>
4
<div class="field">
5
@Html.DropDownList("Employee_No", new SelectList(ViewBag.EmployeeList, "Value", "Text"), LeaveRequestEntryRes.SelectableMessage, new { @class = "ui fluid dropdown" })
6
</div>
7
There are 2 options in the html part. When I select a department, the employees of that department come. My problem is that one of them is always selected. I want to click and select. how do i fix it so that if i click it ?? Sorry for my English. 🙂
Advertisement
Answer
Could you not just set a placeholder option above the loop in the success?
for example:
JavaScript
1
4
1
empName.append('<option value="" selected>Select an option</option>');
2
3
// The loop to populate
4