我认为我有一个剑道 ui 下拉列表:
$("#Instrument").kendoDropDownList({
dataTextField: "symbol",
dataValueField: "symbol",
dataSource: data,
index: 0
});
如何使用 jQuery 更改它的选定值?我试过:
$("#Instrument").val(symbol);
但它没有按预期工作。
我认为我有一个剑道 ui 下拉列表:
$("#Instrument").kendoDropDownList({
dataTextField: "symbol",
dataValueField: "symbol",
dataSource: data,
index: 0
});
如何使用 jQuery 更改它的选定值?我试过:
$("#Instrument").val(symbol);
但它没有按预期工作。
您必须使用 Kendo UI DropDownListselect
方法(此处的文档)。
基本上你应该:
// 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 示例在这里
由于这是与此相关的问题的热门搜索结果之一,我觉得值得一提的是如何使用 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";
});
似乎有一种更简单的方法,至少在 Kendo UI v2015.2.624 中:
$('#myDropDownSelector').data('kendoDropDownList').search('Text value to find');
如果下拉列表中没有匹配项,Kendo 似乎会将下拉列表设置为未选择的值,这是有道理的。
我无法得到@Gang 的工作答案,但如果你用 交换他value
,search
如上所述,我们是黄金。
可以按值“本机”选择:
dropdownlist.select(1);