javascript - 如何通过 jQuery 在 Select2 中获取额外的参数值?

标签 javascript jquery asp.net-mvc jquery-select2

我将一个额外的属性绑定(bind)到 Select2 ddl,并希望通过 jQuery 检索此属性值 (Key),如下所示:

Controller :

public ActionResult GetProjects()
{
    var projects = repository.Projects.Select(m => new ProjectViewModel
    {
        Id = m.Id,
        Name = m.Name,
        Key = m.Key //Extra parameter that I want to retrieve using jQuery                
    })
    return Json(projects, JsonRequestBehavior.AllowGet);
}

我成功填充了ddl,并尝试获取除了Name和Id onChange之外的Key值,但不知道如何获取它:

查看:

<input type="hidden" id="key" name="Key" value=0 /> 

//I fill the ddl without no problem via Select2 and there is no need to post its code for brevity
@Html.DropDownListFor(m => m.ProjectId, Enumerable.Empty<SelectListItem>(), "Select")

$('#ProjectId').change(function (e) {
        projectId = $(this).val(); // I can get the selected ProjectId value
        debugger;

        //!!! But cannot get the Key value
        var key = $('#ProjectId').select2('data').Key;
}

有什么想法吗?

这是 Select2 的渲染 HTML:

<select class="select2-hidden-accessible" data-val="true" data-val-number="The field Proje must be a number." data-val-required="Req" id="ProjectId" name="ProjectId" 
tabindex="-1" aria-hidden="true"><option value="">Select</option>
<option value="1">Project A</option></select>

<span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" 
aria-expanded="false" tabindex="0" aria-labelledby="select2-ProjectId-container"><span class="select2-selection__rendered" id="select2-ProjectId-container" 
title="Project A"><span class="select2-selection__clear">×</span>Project A</span><span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b></span></span></span>

最佳答案

问题的解决办法如下:

<input type="hidden" id="projectPKey" name="ProjectPKey" value=0 /> 

@Html.DropDownListFor(m => m.ProjectId, Enumerable.Empty<SelectListItem>(), "Select" )

projectId.select2({
    //code removed for brevity
    processResults: function (data, page) {
    var newData = [];
    $.each(data, function (index, item) {
        newData.push({            
            id: item.Id, //id part present in data             
            text: item.Description, //string to be displayed
            key: item.Key //extra parameter(s)
        });
    });
});


var pkey = $('#ProjectId').find(':selected').data().data.key;
$('#projectPKey').val(pkey);
console.log(pkey);

关于javascript - 如何通过 jQuery 在 Select2 中获取额外的参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40650286/

相关文章:

javascript - Jquery 数据表 : f is not defined in jquery. dataTables.min.js

jquery - JqChart - 如何在同一页面上显示两个图表?

javascript - 如何从异步调用返回响应?

asp.net-mvc - LabelFor 显示属性名称而不是 Display 属性

javascript - Sencha Touch ExtJS 添加复选框列表

javascript - Rest 服务文档中的 Json 数据的键中包含斜线

asp.net - 在 Dapper 中处理 Oracle 数据库连接

wpf - WPF 和 MVC 是同一个概念吗?

javascript - 嵌套 ng-repeat 中的复选框不可单击

javascript - AJAX调用后如何制作弹出框动画?