【问题标题】:How to Get DropDown Selected Value and This Value Add in List如何获取下拉选择的值和此值添加到列表中
【发布时间】:2016-05-10 05:42:10
【问题描述】:

DropDownList代码:

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

jQueryDropDownList

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

EmployeeListDropDown

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 中删除时,但这不适用于此代码。

【问题讨论】:

  • 您正在删除存储在item 中的文本。是否需要删除$("#EmpID option:selected")
  • 旁注:将您的 veiw 代码更改为 @Html.DropDownListFor(m =&gt; m.EmpID, Model.EmployeeList(), new { @class = "form-control" })(从第一个创建第二个相同的 SelectList 毫无意义)
  • 我不知道存储文本在哪里..

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


【解决方案1】:

使用$('option:selected', this).remove(); 代替你的$("#EmpID").remove(item);

这是working example

【讨论】:

    猜你喜欢
    • 2015-02-09
    • 2022-11-24
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    相关资源
    最近更新 更多