我如何从kendo comboBox中获取选择值

时间:2014-02-20 12:14:22

标签: asp.net-mvc kendo-ui kendo-combobox

我已经实施了Kendo ComboBox,但很难获得选定的价值......

  $("#_FeeScheme_Input").kendoComboBox({
        minLength: 1,
        filter: 'contains',
        dataTextField: "FeeSchemeDescription",
        dataValueField: "FeeSchemeID",
        select: onSelect,
        dataSource: {
            type: "json",
            serverFiltering: false,
            transport: {
                read: "/Qualification/GetAllFeeScheme_JSON"
            },
        }
    });

...

 function onSelect(e) {

        var dataItem = this.dataItem(e.item.index());

        alert("value " + dataItem.text); //NOT WORKING... RETURN NULL VALUE            

    };

剃刀代码

 <div class="form-group">
                @Html.LabelFor(model => model._FeeScheme.FeeSchemeDescription, new { @class = "control-label col-md-3" })
                <div class="col-md-6">
                    @Html.TextBoxFor(model => model._FeeScheme.FeeSchemeDescription, new { id = "_FeeScheme_Input" })
                    @Html.ValidationMessageFor(model => model._FeeScheme.FeeSchemeDescription)
                </div>
 </div>

5 个答案:

答案 0 :(得分:10)

在Asp.net MVC中:

var c = $('#MyCombo');

// to get selected id
c.val() // and also
c.data('kendoComboBox').value()

// to get selected text
c.data('kendoComboBox').text()

// to get selected item index
c.data('kendoComboBox').select()

// to set selected item e.g. 3
c.data('kendoComboBox').select(2)

答案 1 :(得分:4)

kendo comboBox中的getter / setter是kendoComboBox'类'的一部分。

您可以根据需要使用 this.value() this.text()

$("#_FeeScheme_Input").kendoComboBox({
    minLength: 1,
    filter: 'contains',
    dataTextField: "FeeSchemeDescription",
    dataValueField: "FeeSchemeID",        
    dataSource: {
        type: "json",
        serverFiltering: false,
        transport: {
            read: "/Qualification/GetAllFeeScheme_JSON"
        },
    },
    change: function(){
       alert("value " + this.value());
    }
});

答案 2 :(得分:2)

你也可以使用jquery,如果你试图从事件中获取价值。

var CB= $("#_FeeScheme_Input").data("kendoComboBox");
var description= CB.dataItem(CB.select()).FeeSchemeDescription;   // for text field
alert(description);


var CB= $("#_FeeScheme_Input").data("kendoComboBox");
var Id= CB.dataItem(CB.select()).FeeSchemeID;   // for value field
alert(Id);

答案 3 :(得分:2)

答案 4 :(得分:2)

您还可以使用jquery获取ID和值,如下所示:

首先给你的comboBox一个名字:

$("#_FeeScheme_Input").kendoComboBox({
    Name: 'MyComboBox',
    minLength: 1,
    ...

然后你就可以得到这样的ID和值:

var myId = $("#MyComboBox").val();
var myText = $("#MyComboBox").data('kendoComboBox').text();