C# 如何在列表视图中获取kendoui下拉列表选定值的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13151486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the value of a kendoui dropdown list selected value in a listview
提问by user1568738
am having Some kendoui listviews which consists of kendoui dropdown lists and i want to get those dropdown list selected values. To do this am trying,
我有一些由 kendoui 下拉列表组成的 kendoui 列表视图,我想获取这些下拉列表的选定值。要做到这一点,我正在尝试,
$("#cnty1").val();
and here is my dropdownlist,i.e., list of countries coming from Database table,
这是我的下拉列表,即来自数据库表的国家/地区列表,
<input select STYLE="width:90px;height:auto" id ="cnty1" data-bind="value:cnty1"
name="cnty1" data-type="string" data-text-field="cnty"
data-value-field="cntyid" data-source="sourcedata1" class="k-d"
data-role="dropdownlist" />
<span data-for="cnty1" class="k-invalid-msg"></span>
here cnty1 is the id of the dropdown list, but am not getting the value instead am getting "id" of the slected value but not the selected value.
这里 cnty1 是下拉列表的 id,但我没有得到值,而是得到了选定值的“id”,而不是选定值。
And also if the value is not selected am getting the first value id by using $("#cnty1").val();
而且,如果未选择该值,我将通过使用获取第一个值 id $("#cnty1").val();
So, please suggest me a solution so that, 1) I should get only the Selected value and 2) Value of the dropdown list Only if the user selects a value from the list, but don't get the value of the list without selecting.
因此,请建议我一个解决方案,以便,1)我应该只获得 Selected 值和 2)下拉列表的值仅当用户从列表中选择一个值,但不要在没有选择的情况下获取列表的值.
采纳答案by charan
try this one.
试试这个。
<select STYLE="width:90px;height:auto" id ="cnty1" data-bind="value:cnty1"
data-text-field="cnty" data-value-field="cntyid" data-source="sourcedata1" data-role="dropdownlist" data-change="cntySelect" data-option-label="Select"></select>
function cntySelect(e) {
var dropDownVal = $("#cnty1").val();
alert(dropDownVal);
}
回答by Sagar K
Use the following jquery to get selected value/text:
使用以下 jquery 获取选定的值/文本:
For value:$("#cnty1 option:selected").val();
对于价值:$("#cnty1 option:selected").val();
For textuse:$("#cnty1 option:selected").text();
对于文本使用:$("#cnty1 option:selected").text();
回答by Kristo Aun
Although this code is being used for FK JSON objects in KendoUI grid, the idea is similar. You have to bind the object on dropdown value selection. The dropdown essentially contains options that are your value ID's, not the objects themselves. Therefore, you have to iterate through the data source to find which object has been selected and then do the replacement in data model.
尽管此代码用于 KendoUI 网格中的 FK JSON 对象,但其思想是相似的。您必须在下拉值选择时绑定对象。下拉菜单基本上包含您的值 ID 的选项,而不是对象本身。因此,您必须遍历数据源以查找已选择的对象,然后在数据模型中进行替换。
/**
* KendoDropDownEditor Class
* */
var KendoDropDownEditor = Class({
initialize: function (schema, gridId, readUrl) {
var readUrl = readUrl || base + schema.entityName + "/read";
this.dataSource = DataSourceGenerator.crudDS(schema, gridId, readUrl);
this.dataTextField = schema.model.dropDownTextField;
this.dataValueField = schema.model.id;
},
/**
*
* */
do:function (container, options) {
var self = this;
var a = $('<input data-text-field="' + self.dataTextField + '" data-value-field="' + self.dataValueField + '" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
autoBind:false,
dataSource:self.dataSource,
close:function (e) {
$.each(self.dataSource.data(), function(key, value) {
if (value[self.dataValueField] == e.sender.value()) {
options.model[options.field] = value;
return false;
}
});
}
});
}
});
Also look at Knockout-Kendo, it could make your life easier.
也看看Knockout-Kendo,它可以让你的生活更轻松。

