我在视图中有一个kendo ui下拉列表:
$("#Instrument").kendoDropDownList({
dataTextField: "symbol",
dataValueField: "symbol",
dataSource: data,
index: 0
});
$("#Instrument").val(symbol);
但是它并没有像预期的那样工作。
我在视图中有一个kendo ui下拉列表:
$("#Instrument").kendoDropDownList({
dataTextField: "symbol",
dataValueField: "symbol",
dataSource: data,
index: 0
});
$("#Instrument").val(symbol);
但是它并没有像预期的那样工作。
您需要使用Kendo UI DropDownList的 select
方法(文档在这里)。
基本上,您应该:
// get a reference to the dropdown list
var dropdownlist = $("#Instrument").data("kendoDropDownList");
如果你知道索引,你可以使用:
// selects by index
dropdownlist.select(1);
如果不是这样,请使用:
// selects item if its text is equal to "test" using predicate function
dropdownlist.select(function(dataItem) {
return dataItem.symbol === "test";
});
这里有一个JSFiddle的例子,请点击这里
var inst = $("#Instrument").data('kendoDropDownList'); if (inst) { inst.value("一个值"); }
- Jason看起来有一种更简单的方法,至少在Kendo UI v2015.2.624中:
$('#myDropDownSelector').data('kendoDropDownList').search('Text value to find');
我无法让@Gang的答案正常工作,但是如果你将他的value
替换为search
,如上所述,我们就可以了。
search
进行精确匹配。我同意这个函数名字起得不好。对于你的情况,你需要使用原始的data
编写自己的搜索程序,找到一个精确的匹配项[再次通过您自己的搜索程序],然后通过search
函数选择您找到的精确匹配项。听起来不错吧? - ruffin由于这是与此相关的问题的首要搜索结果之一,因此我觉得值得提到如何在Kendo()。DropDownListFor()中使其正常工作。
除了您基于文本和选择器选择项目的方式不同以外,其他所有内容都与OnaBai的帖子相同。
为此,您需要将dataItem.symbol替换为dataItem。[DataTextFieldName]。您用于.DataTextField()的任何模型字段都将与之进行比较。
@(Html.Kendo().DropDownListFor(model => model.Status.StatusId)
.Name("Status.StatusId")
.DataTextField("StatusName")
.DataValueField("StatusId")
.BindTo(...)
)
//So that your ViewModel gets bound properly on the post, naming is a bit
//different and as such you need to replace the periods with underscores
var ddl = $('#Status_StatusId').data('kendoDropDownList');
ddl.select(function(dataItem) {
return dataItem.StatusName === "Active";
});
可以通过“本地方式”按值选择:
dropdownlist.select(1);
dataItem.symbol
。请参考 JS Fiddle http://jsfiddle.net/OnaBai/mRmNJ/。 - OnaBai