如何获取DropDown选定值并将此值添加到列表中

时间:2016-05-10 05:42:10

标签: c# jquery asp.net-mvc-4

DropDownList代码:

<div class="col-md-4">
    @Html.DropDownListFor(
        model => model.EmpID, 
        new SelectList(Model.EmployeeList(), "Value", "Text", Model.EmpID), 
        new { @class = "form-control" }
    )
</div>
jQuery

DropDownList

<script>
    $(document).ready(function () {
        $("#EmpID").change(function () {
            var item = $("#EmpID option:selected").text();
            alert(item)
            $(".test").append(item);
            $("#EmpID").remove(item);
        });
    });
</script>
EmployeeList

DropDown

public List<SelectListItem> EmployeeList()
{
    List<SelectListItem> ProjectList = mp.EmployeeMasters.Select(o => new SelectListItem()
    {
        Text = o.Firstname +" "+ o.Lastname,
        Value = o.EmpID
    }).ToList();
    return ProjectList;
}

每当我选择dropdown值并附加此值并从DrodownList删除,但这不适用于此代码。

1 个答案:

答案 0 :(得分:0)

使用$('option:selected', this).remove();而不是$("#EmpID").remove(item);

这是working example